2014-06-30 208 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