为 nginx 转换 .htaccess 重写规则时出现问题

为 nginx 转换 .htaccess 重写规则时出现问题

大家好,我正在尝试转换以下 .htaccess 代码

<IfModule mod_rewrite.c> 
Options +FollowSymlinks 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^arrowchat/chatroom ^/chatroom/ [L] 
RewriteRule ^arrowchat/cron ^/cron/ [L] 
RewriteRule ^arrowchat/debug ^/debug/ [L] 
RewriteRule ^arrowchat/list ^/list/ [L] 
RewriteRule ^arrowchat/mobile ^/mobile/ [L] 
RewriteRule ^arrowchat/popout ^/popout/ [L] 
RewriteRule ^arrowchat/video ^/video/ [L] 
</IfModule>

这就是我想出的

if ($rule_0 = "2"){
    rewrite ^/arrowchat/chatroom ^/chatroom/ last;
}
    rewrite ^/arrowchat/cron ^/cron/ last;
    rewrite ^/arrowchat/debug ^/debug/ last;
    rewrite ^/arrowchat/list ^/list/ last;
    rewrite ^/arrowchat/mobile ^/mobile/ last;
    rewrite ^/arrowchat/popout ^/popout/ last;
    rewrite ^/arrowchat/video ^/video/ last;

尝试通过运行命令重新加载 nginx conf 文件nginx -s 重新加载我收到一条错误消息

nginx: [emrg] unknown "rule_0" variable

我当前的 nginx default.conf 文件如下所示

#
# The default server
#
server {
    listen       80;
    server_name  jukpac.com;
    return       301 http://www.jukpac.com$request_uri;
}

server {
    listen       80;
    server_name  www.jukpac.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
if ($rule_0 = "2"){
    rewrite ^/arrowchat/chatroom ^/chatroom/ last;
}
    rewrite ^/arrowchat/cron ^/cron/ last;
    rewrite ^/arrowchat/debug ^/debug/ last;
    rewrite ^/arrowchat/list ^/list/ last;
    rewrite ^/arrowchat/mobile ^/mobile/ last;
    rewrite ^/arrowchat/popout ^/popout/ last;
    rewrite ^/arrowchat/video ^/video/ last;
     }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }
    location ~* \.(?:ico|css|js|gif|jpe?g|png|swf)$ {
    expires 30d;
    add_header Pragma public;
    add_header Cache-Control "public";
    } 
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

有人能帮我了解一下重写规则吗?

答案1

将删除一些不必要的东西并清理其余部分default.conf,尝试一下并重新加载 nginx 并告诉我它是否有效。

server { #redirecting server
    listen       80;
    server_name  jukpac.com;
    return       301 http://www.jukpac.com$request_uri;
}

server {
    listen       80;
    server_name  www.jukpac.com;
    root   /usr/share/nginx/html; #move the root to server level
    index  index.php # move index to server level
    error_page  404  /404.html;
    error_page   500 502 503 504  /50x.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~* \.(?:ico|css|js|gif|jpe?g|png|swf)$ {
    expires 30d;
    add_header Pragma public;
    add_header Cache-Control "public";
    } 

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}

相关内容