2014-06-30 290 views
1

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

这是我的配置文件:

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

什么是conf/wordpress.conf'''? – amenadiel

+0

问题已更新 – Mariusz

+0

我没有看到你在哪里试图将backend.php/*重定向到backend.php。另外,为什么你需要重写/ wp-admin。 – amenadiel

回答

1

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

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

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

处理,所以请把它注释掉,而我们整理了其他问题。

下面的规则是不太具体,没有正则表达式,所以它会被处理最后

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

这是罚款。这是任何不是PHP的请求,或者不是一个真正的URL(例如,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结尾的请求,除非它们有一个静态文件扩展名(这不会发生,因为如果他们这样做了,那么它们将不匹配“以php结尾”的东西)。

location ~ \.php$ { 
    ... 
    fastcgi_pass md; 
} 
在这一点上

因此,如果您发出指向带或不带查询字符串/backend.php的请求,并没有与该名一个文件,它会归入.PHP规则传递给你的php-fpm后端。

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

长话短说。如果您需要将backend.php重定向到backend.php的url,则需要设置另一个比.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; 
    } 
} 
+0

它应该放在哪里?我把它放在第二台服务器的开始,nginx启动失败。 – Mariusz

+1

查看我的编辑请 – amenadiel

+0

非常感谢。 – Mariusz