Nginx:除非我做坏事,否则 WordPress 重写不起作用
Nginx: WordPress Rewrites not Working unless I do bad things
我最近刚从 Apache 迁移到 Nginx,我 运行 陷入了 Wordpress 和 Nginx 的困境。目前我是 运行 一个更简单的 Nginx conf,这样你就可以更容易地看到发生了什么,而不必再看 50 多行。
server {
listen 80;
server_name servername.com
root /home/sname/www;
# IF I DON'T DO THIS ALL PAGES APART FROM THE HOMEPAGE DON'T APPEAR!
# Further to this, I use a custom permalink structure %post_name%.
# When turned off the pages work but I can't use custom permalink structure
error_page 404 /index.php;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# PHP Handler
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
我在 HTTP 块中有所有常用的:
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
index index.php index.html index.htm;
include /etc/nginx/conf.d/*.conf;
为什么我不能忍受这个?那么这里是一个站点范围的例子。 (由于 URL 错误和 link 限制,已替换为站点名称!)
http://www.pro-tradesouthwales.co.uk/services/damp-proofing/ <-- 我要服务的页面
sitename!/services/damp-proofing <--- 不显示 404(以下所有显示空白页)
sitename!/services/damp-proofing/index.php <--- 不显示 404 或重定向
sitename!/services/damp-proofing/index.php/ <--- 好吧,这很糟糕!
站点名称!/services/damp-proofing/index.php/index.php <--- 天哪!
感谢所有帮助。我已经尝试了很多不同的设置,并试图让它工作好几天。请不要说阅读指南之类的话来光顾我,我吃了该死的 nginx 字典仍然无济于事:)
谢谢大家!
它在一定程度上起作用了。原来 WordPress 只是有问题,但据我所知,这是设置 WordPress 网站的最 "correct" 方式:)
希望对其他人也有帮助。请记住,事情会根据您的服务器及其独特的设置而变化。所以您的 PHP 设置可能不同。例如 php-fpm 目录并不总是存在,有时也会创建为 php5-fpm。如果有疑问,请使用 shell(使用 putty 或类似的东西通过 SSH 连接到您的服务器)并找到 php-fpm.sock.
又名:
光盘/等/
目录-a
---- 显示文件列表 ----
...
等等
server {
listen 80;
server_name www.pro-tradesouthwales.co.uk;
root /home/protrade/www;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# START: Solution
location / {
try_files $uri $uri/ /index.php?$args;
}
# END: Solution
location ~ /\. {
deny all;
}
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(js|css|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)$ {
expires max;
log_not_found off;
}
location ~* \.()$ {
log_not_found off;
expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
我最近刚从 Apache 迁移到 Nginx,我 运行 陷入了 Wordpress 和 Nginx 的困境。目前我是 运行 一个更简单的 Nginx conf,这样你就可以更容易地看到发生了什么,而不必再看 50 多行。
server {
listen 80;
server_name servername.com
root /home/sname/www;
# IF I DON'T DO THIS ALL PAGES APART FROM THE HOMEPAGE DON'T APPEAR!
# Further to this, I use a custom permalink structure %post_name%.
# When turned off the pages work but I can't use custom permalink structure
error_page 404 /index.php;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# PHP Handler
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
我在 HTTP 块中有所有常用的:
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
index index.php index.html index.htm;
include /etc/nginx/conf.d/*.conf;
为什么我不能忍受这个?那么这里是一个站点范围的例子。 (由于 URL 错误和 link 限制,已替换为站点名称!)
http://www.pro-tradesouthwales.co.uk/services/damp-proofing/ <-- 我要服务的页面 sitename!/services/damp-proofing <--- 不显示 404(以下所有显示空白页) sitename!/services/damp-proofing/index.php <--- 不显示 404 或重定向 sitename!/services/damp-proofing/index.php/ <--- 好吧,这很糟糕!
站点名称!/services/damp-proofing/index.php/index.php <--- 天哪!
感谢所有帮助。我已经尝试了很多不同的设置,并试图让它工作好几天。请不要说阅读指南之类的话来光顾我,我吃了该死的 nginx 字典仍然无济于事:)
谢谢大家!
它在一定程度上起作用了。原来 WordPress 只是有问题,但据我所知,这是设置 WordPress 网站的最 "correct" 方式:)
希望对其他人也有帮助。请记住,事情会根据您的服务器及其独特的设置而变化。所以您的 PHP 设置可能不同。例如 php-fpm 目录并不总是存在,有时也会创建为 php5-fpm。如果有疑问,请使用 shell(使用 putty 或类似的东西通过 SSH 连接到您的服务器)并找到 php-fpm.sock.
又名: 光盘/等/ 目录-a ---- 显示文件列表 ---- ... 等等
server {
listen 80;
server_name www.pro-tradesouthwales.co.uk;
root /home/protrade/www;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# START: Solution
location / {
try_files $uri $uri/ /index.php?$args;
}
# END: Solution
location ~ /\. {
deny all;
}
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(js|css|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)$ {
expires max;
log_not_found off;
}
location ~* \.()$ {
log_not_found off;
expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}