You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将CRM的两个.htaccess规则转换为Nginx配置并集成到CloudPanel虚拟主机文件中求助

将CRM的两个.htaccess规则转换为Nginx配置并集成到CloudPanel虚拟主机文件中求助

嘿,我来帮你搞定这个从Apache .htaccess转Nginx配置的问题!从Apache切换到Nginx确实需要适应一下规则写法,我会一步步帮你把两个.htaccess的逻辑转换成Nginx配置,再整合到你的CloudPanel虚拟主机文件里。

先拆解两个.htaccess的核心规则

首先我们先把每个.htaccess里的功能对应到Nginx的写法:

1. 根目录.htaccess的规则转换

  • 关闭目录索引(对应Options -Indexes):Nginx用autoindex off;实现,直接加到server块里就行;MultiViews在Nginx里没有直接等效项,默认不会开启类似功能,不用额外处理。
  • 传递Authorization头:在PHP处理的location块里添加fastcgi_param HTTP_AUTHORIZATION $http_authorization;,确保认证信息能传给PHP程序。
  • 重定向尾部斜杠:仅当请求的不是目录时,去掉尾部斜杠并301重定向:
    if (!-d $request_filename) {
        rewrite ^/(.*)/$ /$1 permanent;
    }
    
  • 转发非静态文件到根index.php:先排除指定后缀的静态文件,剩下的非文件/非目录请求转发到根index.php:
  • 把特定目录转发到public下:单独写location规则,匹配指定目录,将请求转发到public对应的路径:
    location ~ ^/(css|assets|landing|storage|installer|js|custom)/(.*)$ {
        try_files $uri /public/$1/$2 last;
    }
    

2. /public目录.htaccess的规则转换

这个目录的核心是处理自身的路由,需要针对/public路径单独写规则:

location /public/ {
    autoindex off;
    # 重定向尾部斜杠
    if (!-d $request_filename) {
        rewrite ^/public/(.*)/$ /public/$1 permanent;
    }
    # 非文件/非目录请求转发到public/index.php
    if (!-e $request_filename) {
        rewrite ^/public/(.*)$ /public/index.php last;
    }
}

整合到你的CloudPanel虚拟主机文件

现在把这些规则嵌入到你现有的两个server块中,注意保留CloudPanel的模板变量(比如{{root}}、{{php_fpm_port}}):

第一个server块(8080端口,内部PHP处理)

修改后完整配置:

server {
    listen 8080;
    listen [::]:8080;
    server_name crm.deppastudios.it;
    {{root}}
    autoindex off; # 对应Options -Indexes
    index index.php index.html;

    # 重定向尾部斜杠
    if (!-d $request_filename) {
        rewrite ^/(.*)/$ /$1 permanent;
    }

    # 转发特定目录到public下
    location ~ ^/(css|assets|landing|storage|installer|js|custom)/(.*)$ {
        try_files $uri /public/$1/$2 last;
    }

    location / {
        # 排除静态文件,直接返回
        if ($uri ~* \.(css|js|png|jpg|jpeg|gif|robots\.txt|ico|woff|woff2|ttf|svg)$) {
            break;
        }
        # 非文件/非目录请求转发到根index.php
        if (!-e $request_filename) {
            rewrite ^ /index.php last;
        }
        try_files $uri $uri/ /index.php?$args;
    }

    # public目录路由规则
    location /public/ {
        autoindex off;
        if (!-d $request_filename) {
            rewrite ^/public/(.*)/$ /public/$1 permanent;
        }
        if (!-e $request_filename) {
            rewrite ^/public/(.*)$ /public/index.php last;
        }
        try_files $uri $uri/ /public/index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        # 传递Authorization头
        fastcgi_param HTTP_AUTHORIZATION $http_authorization;
        try_files $uri =404;
        fastcgi_read_timeout 3600;
        fastcgi_send_timeout 3600;
        fastcgi_param HTTPS "on";
        fastcgi_param SERVER_PORT 443;
        fastcgi_pass 127.0.0.1:{{php_fpm_port}};
        fastcgi_param PHP_VALUE "{{php_settings}}";
    }

    if (-f $request_filename) {
        break;
    }
}

第二个server块(80/443端口,对外访问)

修改后完整配置:

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    {{ssl_certificate_key}}
    {{ssl_certificate}}
    server_name crm.deppastudios.it;
    {{root}}
    {{nginx_access_log}}
    {{nginx_error_log}}
    autoindex off;

    # HTTPS强制跳转
    if ($scheme != "https") {
        rewrite ^ https://$host$uri permanent;
    }

    # 重定向尾部斜杠
    if (!-d $request_filename) {
        rewrite ^/(.*)/$ /$1 permanent;
    }

    # 转发特定目录到public下
    location ~ ^/(css|assets|landing|storage|installer|js|custom)/(.*)$ {
        try_files $uri /public/$1/$2 last;
    }

    location / {
        {{varnish_proxy_pass}}
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_hide_header X-Varnish;
        proxy_redirect off;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      720;
        proxy_send_timeout         720;
        proxy_read_timeout         720;
        proxy_buffer_size          128k;
        proxy_buffers              4 256k;
        proxy_busy_buffers_size    256k;
        proxy_temp_file_write_size 256k;

        # 非文件/非目录请求转发到根index.php
        if (!-e $request_filename) {
            rewrite ^ /index.php last;
        }
    }

    # public目录路由规则
    location /public/ {
        autoindex off;
        if (!-d $request_filename) {
            rewrite ^/public/(.*)/$ /public/$1 permanent;
        }
        if (!-e $request_filename) {
            rewrite ^/public/(.*)$ /public/index.php last;
        }
    }

    location ~ /.well-known {
        auth_basic off;
        allow all;
    }

    {{settings}}

    location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|woff2|eot|mp4|ogg|ogv|webm|webp|zip|swf|map)$ {
        add_header Access-Control-Allow-Origin "*";
        expires max;
        access_log off;
    }

    if (-f $request_filename) {
        break;
    }
}

最后几步验证

  1. 修改完配置后,先执行nginx -t测试配置语法是否合法,避免报错
  2. 测试通过后,重启Nginx服务(CloudPanel后台有重启按钮,或者用命令systemctl restart nginx)
  3. 如果遇到权限问题,确保CRM的文件目录权限正确,Nginx用户(一般是www-data)有读写权限

备注:内容来源于stack exchange,提问作者johnnydeppa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 14:02:43