nginx wordpress永久链接天和名字

大家好:)我是nginx的新手,我正在尝试设置通常会添加到.htaccess文件的wordpress永久链接结构。

这里是.htacces的旧的重写规则

# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress 

这里是当前的nginx站点configuration:

 server { listen 80; server_name *.noconformity.co; rewrite ^ http://noconformity.co$request_uri? permanent; } server { listen 80; server_name noconformity.co ~(sub1|sub2)\.noconformity\.co$; root /srv/www/www.noconformity.co; access_log /var/log/nginx-hosts/www_noconformity.co.access; error_log /var/log/nginx-hosts/www_noconformity.co.error error; error_page 400 401 402 403 404 500 502 503 504 /error.htm; location /error.htm { internal; } # ------------------------------------------------------------------------ location / { index index.html index.php index.htm; location ~* ^.*\.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } # ------------------------------------------------------------------------ # serve favicon location = /favicon.ico { log_not_found off; access_log off; } # ------------------------------------------------------------------------ # process robots.txt location = /robots.txt { allow all; log_not_found off; access_log off; } # ------------------------------------------------------------------------ # serve static files directly location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log off; expires max; } # ------------------------------------------------------------------------ # block access to .ht files location ~ /\.ht { deny all; } # ------------------------------------------------------------------------ # short cuts if ($uri ~* "/login") { rewrite ^/login(/.*)? /wp-admin$1; } # ------------------------------------------------------------------------ # BEGIN W3TC Browser Cache gzip on; gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon; location ~ \.(css|js)$ { expires 31536000s; add_header Pragma "public"; add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"; add_header X-Powered-By "W3 Total Cache/0.9.2.3"; } location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ { expires 180s; add_header Pragma "public"; add_header Cache-Control "max-age=180, public, must-revalidate, proxy-revalidate"; add_header X-Powered-By "W3 Total Cache/0.9.2.3"; } location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ { expires 31536000s; add_header Pragma "public"; add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"; add_header X-Powered-By "W3 Total Cache/0.9.2.3"; } # END W3TC Browser Cache # BEGIN W3TC Skip 404 error handling by WordPress for static files if (-f $request_filename) { break; } if (-d $request_filename) { break; } if ($request_uri ~ "(robots\.txt|sitemap(_index|[0-9]+)?\.xml(\.gz)?)") { break; } if ($request_uri ~* \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$) { return 404; } # END W3TC Skip 404 error handling by WordPress for static files # ------------------------------------------------------------------------ } 

我遇到了几个类似的例子,但没有任何工作到目前为止。 任何指针?

如果可能的if你应该避免使用if指令。 上面的Apache的重写规则可以转换为Nginx的语法如下:

 try_files $uri $uri/ /index.php; 

关于Nginx的Wordpress已经有了详细的文档: http : //wiki.nginx.org/Wordpress

没有理由尝试重新发明轮子,当然也不需要兼容模块。

如果你只是想让WordPress的永久链接与nginx一起工作,你可以尝试使用WP的插件http://wordpress.org/extend/plugins/nginx-compatibility/

经过了一吨更多的研究,我可以通过使用下面的工作得到它的工作。 这是域的configuration:

 server { listen 80; server_name *.noconformity.co noconformity.co; root /srv/www/www.noconformity.co; access_log /var/log/nginx-hosts/www_noconformity.co.access; error_log /var/log/nginx-hosts/www_noconformity.co.error error; error_page 400 401 402 403 404 500 502 503 504 /error.htm; location /error.htm { internal; } # ------------------------------------------------------------------------ # remove all sub domains and www if ($http_host !~* "^(sub1|sub2)\.noconformity\.co$"){ set $rule_0 1$rule_0; } if ($http_host ~ "^([^.]+)\.noconformity\.co$"){ set $rule_0 2$rule_0; } if ($rule_0 = "21"){ rewrite ^/(.*)$ http://noconformity.co/$1 permanent; break; } include /etc/nginx/sites-available/wordpress.conf; } 

包括所有的共享WordPressconfiguration:

 # ------------------------------------------------------------------------ location / { index index.html index.php index.htm; location ~* ^.*\.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } try_files $uri $uri/ /index.php?$args; } # ------------------------------------------------------------------------ # serve favicon location = /favicon.ico { log_not_found off; access_log off; } # ------------------------------------------------------------------------ # process robots.txt location = /robots.txt { allow all; log_not_found off; access_log off; } # ------------------------------------------------------------------------ # serve static files directly location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log off; expires max; } # ------------------------------------------------------------------------ # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). location ~ /\. { deny all; access_log off; log_not_found off; } # ------------------------------------------------------------------------ # short cuts if ($uri ~* "/login") { rewrite ^/login(/.*)? /wp-admin$1; } # ------------------------------------------------------------------------ # BEGIN W3TC Browser Cache gzip on; gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon; location ~ \.(css|js)$ { expires 31536000s; add_header Pragma "public"; add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"; add_header X-Powered-By "W3 Total Cache/0.9.2.3"; } location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ { expires 180s; add_header Pragma "public"; add_header Cache-Control "max-age=180, public, must-revalidate, proxy-revalidate"; add_header X-Powered-By "W3 Total Cache/0.9.2.3"; } location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ { expires 31536000s; add_header Pragma "public"; add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"; add_header X-Powered-By "W3 Total Cache/0.9.2.3"; } # END W3TC Browser Cache # BEGIN W3TC Skip 404 error handling by WordPress for static files if (-f $request_filename) { break; } if (-d $request_filename) { break; } if ($request_uri ~ "(robots\.txt|sitemap(_index|[0-9]+)?\.xml(\.gz)?)") { break; } if ($request_uri ~* \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$) { return 404; } # END W3TC Skip 404 error handling by WordPress for static files # ------------------------------------------------------------------------ 

基本上我添加了下面的块:

 try_files $uri $uri/ /index.php?$args; 

到位置块:

 # ------------------------------------------------------------------------ location / { index index.html index.php index.htm; location ~* ^.*\.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } try_files $uri $uri/ /index.php?$args; } 

这可以做得更好吗? 请让我知道,我喜欢学习更多关于nginx

经过进一步的审查,我已经更新了我的答案,基于http://wiki.nginx.org/WordPress的以下设&#x7F6E;

网站configuration:

 server { listen 80; server_name *.noconformity.co noconformity.co; root /srv/www/www.noconformity.co; access_log /var/log/nginx-hosts/www_noconformity.co.access; error_log /var/log/nginx-hosts/www_noconformity.co.error error; error_page 400 401 402 403 404 500 502 503 504 /error.htm; location /error.htm { internal; } # ------------------------------------------------------------------------ # remove all sub domains and www if ($http_host !~* "^(sub1|sub2)\.noconformity\.co$"){ set $rule_0 1$rule_0; } if ($http_host ~ "^([^.]+)\.noconformity\.co$"){ set $rule_0 2$rule_0; } if ($rule_0 = "21"){ rewrite ^/(.*)$ http://noconformity.co/$1 permanent; break; } include /etc/nginx/sites-available/wordpress.conf; } 

和wordpress.conf

 # ------------------------------------------------------------------------ location / { index index.html index.php index.htm; location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_intercept_errors on; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } try_files $uri $uri/ /index.php?$args; } # ------------------------------------------------------------------------ # Login Short Cut if ($uri ~* "/login") { rewrite ^/login(/.*)? /wp-admin$1; } # ------------------------------------------------------------------------ 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|png|jpg|jpeg|gif|ico)$ { expires max; log_not_found off; } # ------------------------------------------------------------------------ # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). location ~ /\. { deny all; access_log off; log_not_found off; } 

请让我知道你的想法,我真的在寻找反馈。 如果你有任何build议,请让我知道要改变的细节。