在nginx中重定向

时间:2014-06-30 13:04:05

标签: nginx

我知道有很多类似的问题,但我尝试了几种解决方案,但我找不到正确的解决方案。 我不知道nginx。我只有一个简单的任务:在一个具体的应用程序/网站中将所有地址/backend.php/*重定向到/backend.php。我使用*来表达任何内容。现在/backend.php/*路径被重定向到/index.php

这是我的配置文件:

server {
        server_name  _;
        rewrite ^ $scheme://mysite.com$request_uri redirect;
}

upstream md {
        #this should match value of "listen" directive in php-fpm pool
        server unix:/var/run/md.php5-fpm.sock;
}

server
{
    server_name .mydomain.eu .mydomain.du;

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

    root /home/md/;

    include conf/restrictions.conf;
    include conf/wordpress.conf; 
    # Pass all .php files onto a php-fpm/php-fcgi server.
    location ~ \.php$ {
        # Zero-day exploit defense.
        # http://forum.nginx.org/read.php?2,88845,page=3
        # Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
        # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine.  And then cross your fingers that you won't get hacked.
        try_files $uri =404;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        include fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #   fastcgi_intercept_errors on;
        fastcgi_pass md;
    }
}

======= UPDATE ===========

CONF / wordpress.conf:

# WordPress single blog rules.
# Designed to be included in any server {} block.

# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
        try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
       access_log off; log_not_found off; expires max;
}

# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;

## Pass all .php files onto a php-fpm/php-fcgi server.
#location ~ \.php$ {
#       # Zero-day exploit defense.
#       # http://forum.nginx.org/read.php?2,88845,page=3
#       # Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
#       # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine.  And then cross your fingers that you won't get hacked.
#       try_files $uri =404;
#
#       fastcgi_split_path_info ^(.+\.php)(/.+)$;
#       #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#       include fastcgi_params;
#       fastcgi_index index.php;
#       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
##      fastcgi_intercept_errors on;
##      fastcgi_pass wp-php;
#}

1 个答案:

答案 0 :(得分:1)

确定。 Nginx请求规则首先对正则表达式进行操作,从更具体到更不具体。然后对非正则表达式规则进行操作。

在你的情况下,老实说,我不知道它的顺序是什么

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

处理完毕,请在我们对其他问题进行排序时对其进行评论。

以下规则不太具体,没有正则表达式,因此最后会处理

location / {
        try_files $uri $uri/ /index.php?$args;
}

哪个好。对于任何不是php或不是真实网址的请求(例如,wordpress不错的网址),它都是后备。

以下规则具有正则表达式并且非常具体,因此它将首先处理,而且如您所见,它会影响静态文件:

location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
       access_log off; log_not_found off; expires max;
}

最后,php规则具有正则表达式并且不像前一个那样具体,因此它将被处理为以.php结尾的请求,除非它们具有静态文件扩展名(因为如果它们不会发生那么,他们就不会与#34结束了#34;

location ~ \.php$ {
    ...
    fastcgi_pass md;
}

所以此时,如果您发出的请求指向带有或不带查询字符串的/backend.php,而存在具有该名称的文件,则它将属于.php规则并传递给你的php-fpm后端。

如果您发出的请求指向/backend.php/something且没有该名称的文件夹,则它将属于第一条规则,因为不存在&#39 ; ta backend.php文件夹,它将被重定向(通过try_files指令)到index.php。

长话短说。如果您需要将backend.php重定向到backend.php的网址,则需要设置另一个比.php更具体的规则。

编辑:只是为了丢弃可能的错误,请注释掉您包含conf / wordpress.conf的行。相反,您的第二个服务器块应该读取

server
{
    server_name .mydomain.eu .mydomain.du;

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

    root /home/md/;

    include conf/restrictions.conf;

    location / {
            try_files $uri $uri/ /index.php?$args;
    }

    # Directives to send expires headers and turn off 404 error logging.
    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
           access_log off; log_not_found off; expires max;
    }

    location ~ ^/backend\.php/(.*)$ {
        try_files $uri /backend.php?$1;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass md;
    }
}