nginx中的重定向

nginx中的重定向,nginx,Nginx,我知道有很多类似的问题,但我尝试了几种解决方案,却找不到正确的解决方案。 我不知道nginx。我只有一个简单的任务:在一个具体的应用程序/网站中将所有地址重定向到/backend.php/*。我用*来表达任何东西。现在/backend.php/*路径被重定向到/index.php 这是我的配置文件: server { server_name _; rewrite ^ $scheme://mysite.com$request_uri redirect; } up

我知道有很多类似的问题,但我尝试了几种解决方案,却找不到正确的解决方案。 我不知道nginx。我只有一个简单的任务:在一个具体的应用程序/网站中将所有地址重定向到
/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;
    }
}
==========更新==============

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;
#}

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

在你的情况下,我真的不知道顺序是什么

rewrite /wp-admin$ $scheme://$host$uri/ permanent;
已处理,请在我们整理其他问题时对其进行注释

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

location / {
        try_files $uri $uri/ /index.php?$args;
}
这很好。对于任何非php或非真实url(例如wordpress nice url)的请求,它都是一种回退

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

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规则有regex,并且没有前一个规则那么具体,因此将对以.php结尾的请求进行处理,除非它们具有静态文件扩展名(这不会发生,因为如果它们具有静态文件扩展名,那么它们将不匹配“以php结尾”的内容)

因此,在这一点上,如果您发出一个指向/backend.php(带或不带查询字符串)的请求,并且有一个同名文件,它将属于.php规则并传递到您的php fpm后端

如果您发出一个指向/backend.php/something且没有同名文件夹的请求,它将属于第一条规则,并且由于没有backend.php文件夹,它将被重定向(通过try_files指令)到index.php

长话短说。如果需要将包含backend.php的URL重定向到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;
    }
}

include
conf/wordpress.conf
中有什么内容?我看不出您试图将backend.php/*重定向到backend.php的位置。另外,为什么需要重写/wp-admin。另外,您计划在后端重定向什么样的url?是否有backend.php文件?有backend.php文件夹吗?我附带了conf文件,没有重定向。我需要重定向,我不知道怎么做。我也不知道为什么会有rewrite/wp admin。我不是这个的管理员或开发者。我只需要做有问题的重定向。我想有backend.php文件。它应该放在哪里?我把它放在第二台服务器的开头,nginx在启动时失败。
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;
    }
}