繁体   English   中英

Nginx重写规则有问题

[英]Trouble with Nginx Rewrite Rules

我对Nginx相当陌生,我正在致力于将.htaccess文件转换为nginx可以理解的东西。 一切工作正常(大部分情况下)-我可以很好地调出主页。 问题是当我进入帖子页面时。

认为与wordpress类似,URL如:

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

其中12是发布ID,并且显然该字符串是发布子代码。 我试图将它们解析为两个单独的参数(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=$1&slug=$2 last;
                    }
        }

}

如果将单个帖子解析为id和slug并将它们传递给您,请让我知道您是否发现了什么。 谢谢!

您应该添加/在开始和/像这样的index.php之前:

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

注意我也使用了$1$2

如果您发布的确实是COMPLETE配置文件,则该设置缺少处理PHP文件的内容,因为regexp看起来还不错。

我实际上认为您发布的配置不能是完整的配置,或者发生了一些根本的事情,因为该配置应该抛出错误并无法加载,而且,因为您提到您的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=$1&slug=$2 last;
    }       
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM