Nginx 重写规则的问题

Trouble with Nginx Rewrite Rules

我是 Nginx 的新手,我正在努力将 .htaccess 文件转换成 Nginx 可以理解的内容。一切都运行良好(大部分)——我可以很好地调出主页。问题是当我到达 post 页面时。

认为与 wordpress 相似,URL 如:

http://www.example.com/12/post-title-in-slug-form

其中 12 是 post id,显然该字符串是 post slug。我试图将它们解析为两个单独的参数(id 和 slug)并将它们传递给 index.php 就像我在 apache 中成功做的那样。不过,我得到了一个 404 页面,并且已经确认这是因为重写规则。这是整个配置文件的样子,只更改了网站名称(出于隐私考虑):

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;


        server {
                    listen 80;
                    server_name example.com;

                    access_log off;
                    error_log on;

                    # deny access to .XYZ files
                    location ~ /\. {
                        return 403;
                    }

                    location ~ sitemap\.xml {
                        return 301  http://example.com/sitemap.php;
                    }

                    location ~ .php$ {
                        # Here you have to decide how to handle php
                        # Generic example configs below
                        # Uncomment and fix up one of the two options

                        # Option 1: Use FastCGI
                        fastcgi_index index.php;
                        include fastcgi_params;
                        fastcgi_pass unix:/var/run/php5-fpm.sock;
                    }

                    location / {
                        try_files $uri $uri/ @router;
                    }

                    location @router {
                        rewrite ^/([0-9]+)/?(.*)?/?$ /index.php?id=&slug= last;
                    }
        }

}

请让我知道在将单个 post 解析为 id 和 slug 并传递它们时,您是否能发现问题所在。谢谢!

您应该在开头添加一个 /,在 index.php 之前添加一个 /,如下所示:

rewrite ^/([0-9]+)/?(.*)?/?$ /index.php?id=&slug= last;

注意我还使用了</code>和<code>

如果您发布的确实是完整的配置文件,那么设置缺少处理 PHP 文件的内容,因为正则表达式看起来没问题。

我实际上认为你发布的配置不可能是完整的,或者有一些基本的配置应该抛出错误并且加载失败,而且,因为你提到你的 PHP加载正常,那么它不能是为您的网站服务的已发布配置。

下面附上一个更好的配置: 仅供参考,try_files ABC XYZ last; 不是有效语法,您至少需要 try_files 中的两个选项。无论如何,也修复了发布的配置中的那些。

server {
    listen 80;
    server_name example.com;

    access_log off;
    error_log on;

    # deny access to .XYZ files
    location ~ /\. {
        return 403;
    }

    location ~ sitemap\.xml {
        return 301  http://example.com/sitemap.php;
    }

    location ~ .php$ {
        # Here you have to decide how to handle php
        # Generic example configs below
        # Uncomment and fix up one of the two options

        # Option 1: Use FastCGI
        #fastcgi_index index.php;
        #include fastcgi_params;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;

        # Option 2: Pass to Apache
        # Proxy_pass localhost:APACHE_PORT

    }

    location / {
        try_files $uri $uri/ @router;
    }

    location @router {
        rewrite ^/([0-9]+)/?(.*)/?$ /index.php?id=&slug= last;
    }       
}

您需要修复 PHP 处理位并选择要实施的设置。

我认为您需要验证您只有一个 nginx 实例 运行 并且它正在为您的网站提供服务。