{"id":156,"date":"2016-11-20T20:24:54","date_gmt":"2016-11-20T12:24:54","guid":{"rendered":"https:\/\/blog.freesilo.com\/?p=156"},"modified":"2016-11-20T21:08:45","modified_gmt":"2016-11-20T13:08:45","slug":"wordpress-nginx-%e7%9a%84-conf-%e5%ae%8c%e7%be%8e%e9%85%8d%e7%bd%ae%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/freesilo.com\/?p=156","title":{"rendered":"WordPress + Nginx \u7684 CONF \u5b8c\u7f8e\u914d\u7f6e\u65b9\u6cd5"},"content":{"rendered":"<p>WordPress + Nginx \u914d\u7f6e\u65b9\u6cd5\uff1a<a href=\"https:\/\/codex.wordpress.org\/Nginx\">https:\/\/codex.wordpress.org\/Nginx<\/a><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd WordPress + Nginx \u7684 CONF \u5b8c\u7f8e\u914d\u7f6e\u65b9\u6cd5 \uff0c\u6240\u6709\u7684 WordPress \u7248\u672c\u901a\u7528\u3002\u5305\u62ec\u4e14\u4e0d\u9650\u4e8e WordPress \u5355\u535a\u5ba2\u7ad9\u70b9\u3001\u591a\u535a\u5ba2+\u5b50\u76ee\u5f55\u3001\u591a\u535a\u5ba2+\u5b50\u57df\u540d\u7b49\u3002\u4ee5\u4e0b\u914d\u7f6e\u6587\u4ef6\u5728 Nginx \u5728 0.8 \u7248\u672c\u4e0b\u6d4b\u8bd5\u901a\u8fc7\u3002<span id=\"more-410\"><\/span>\uff11\u3001\/etc\/nginx\/nginx.conf\u3000\u4e3b\u6587\u4ef6\u914d\u7f6e\u3001\u5173\u4e8e\u6027\u80fd\u7b49 Nginx \u53c2\u6570\u914d\u7f6e<\/p>\n<blockquote><p># Generic startup file.<\/p>\n<p>user {user} {group};<\/p>\n<p>worker_processes 2;<\/p>\n<p>error_log \/var\/log\/nginx\/error.log;<\/p>\n<p>pid \/var\/run\/nginx.pid;<\/p>\n<p># Keeps the logs free of messages about not being able to bind().<\/p>\n<p>#daemon off;<\/p>\n<p>events {<\/p>\n<p>worker_connections 1024;<\/p>\n<p>}<\/p>\n<p>http {<\/p>\n<p># rewrite_log on;<\/p>\n<p>include mime.types;<\/p>\n<p>default_type application\/octet-stream;<\/p>\n<p>access_log \/var\/log\/nginx\/access.log;<\/p>\n<p>sendfile on;<\/p>\n<p># tcp_nopush on;<\/p>\n<p>keepalive_timeout 3;<\/p>\n<p># tcp_nodelay on;<\/p>\n<p># gzip on;<\/p>\n<p>client_max_body_size 13m;<\/p>\n<p>index index.php index.html index.htm;<\/p>\n<p># Upstream to abstract backend connection(s) for PHP.<\/p>\n<p>upstream php {<\/p>\n<p>server unix:\/tmp\/php-fpm.sock;<\/p>\n<p># server 127.0.0.1:9000;<\/p>\n<p>}<\/p>\n<p>include sites-enabled\/*; \u00a0\u3000\u6ce8\uff1a\u8fd9\u91cc\u53ef\u63d2\u5165 server {} \u90e8\u5206\u3002\u6216\u8005\u518d\u53e6\u8d77\u65b0\u6587\u4ef6\u3001\u88c5\u5165\u3002<\/p>\n<p>}<\/p><\/blockquote>\n<p>2\u3001\u7ad9\u70b9\u7684 server \u6587\u4ef6\u914d\u7f6e<\/p>\n<blockquote><p># Redirect everything to the main site.<\/p>\n<p>server {<\/p>\n<p>server_name *.mysite.com;<\/p>\n<p>root \/var\/www\/mysite.com;<\/p>\n<p>if ($http_host != \u201cmysite.com\u201d) {<\/p>\n<p>rewrite ^ http:\/\/mysite.com$request_uri permanent;<\/p>\n<p>}<\/p>\n<p>#include global\/restrictions.conf;<\/p>\n<p>#\/\/ Additional rules go here.<\/p>\n<p>#\/\/ Only include one of the files below. \u00a0\uff08\u4ee5\u4e0b\u4e09\u9009\u4e00\uff09<\/p>\n<p># include global\/wordpress.conf; \u00a0\uff08\u5355\u535a\u5ba2\uff09<\/p>\n<p># include global\/wordpress-ms-subdir.conf;\u3000\uff08\u591a\u535a\u5ba2\u3001\u5b50\u76ee\u5f55\uff09<\/p>\n<p># include global\/wordpress-ms-subdomain.conf; \u00a0\uff08\u591a\u535a\u5ba2\u3001\u5b50\u57df\u540d\uff09<\/p>\n<p>}<\/p><\/blockquote>\n<p>\uff13\u3001\u901a\u7528\u90e8\u4efd<\/p>\n<blockquote><p># Global restrictions configuration file.<\/p>\n<p># Designed to be included in any server {} block.&lt;\/p&gt;<\/p>\n<p>location = \/favicon.ico {<\/p>\n<p>log_not_found off;<\/p>\n<p>access_log off;<\/p>\n<p>}<\/p>\n<p>location = \/robots.txt {<\/p>\n<p>allow all;<\/p>\n<p>log_not_found off;<\/p>\n<p>access_log off;<\/p>\n<p>}<\/p>\n<p># Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).<\/p>\n<p>location ~ \/\\. {<\/p>\n<p>deny all;<\/p>\n<p>access_log off;<\/p>\n<p>log_not_found off;<\/p>\n<p>}<\/p>\n<p># Deny access to any files with a .php extension in the uploads directory<\/p>\n<p>location ~* ^\/wp-content\/uploads\/.*.php$ {<\/p>\n<p>deny all;<\/p>\n<p>access_log off;<\/p>\n<p>log_not_found off;<\/p>\n<p>}<\/p>\n<p># Deny access to any files with a .php extension in the uploads directory for multisite<\/p>\n<p>location ~* \/files\/(.*).php$ {<\/p>\n<p>deny all;<\/p>\n<p>access_log off;<\/p>\n<p>log_not_found off;<\/p>\n<p>}<\/p><\/blockquote>\n<p>\uff14\u3001\u5355\u535a\u5ba2<\/p>\n<blockquote><p># WordPress single blog rules.<\/p>\n<p># Designed to be included in any server {} block.<\/p>\n<p># This order might seem weird \u2013 this is attempted to match last if rules below fail.<\/p>\n<p># http:\/\/wiki.nginx.org\/HttpCoreModule<\/p>\n<p>location \/ {<\/p>\n<p>try_files $uri $uri\/ \/index.php?$args;<\/p>\n<p>}<\/p>\n<p># Add trailing slash to *\/wp-admin requests.<\/p>\n<p>rewrite \/wp-admin$ $scheme:\/\/$host$uri\/ permanent;<\/p>\n<p># Directives to send expires headers and turn off 404 error logging.<\/p>\n<p>location ~* \\.(js|css|png|jpg|jpeg|gif|ico)$ {<\/p>\n<p>expires 24h;<\/p>\n<p>log_not_found off;<\/p>\n<p>}<\/p>\n<p># Uncomment one of the lines below for the appropriate caching plugin (if used).<\/p>\n<p>#include global\/wordpress-wp-super-cache.conf;<\/p>\n<p>#include global\/wordpress-w3-total-cache.conf;<\/p>\n<p># Pass all .php files onto a php-fpm\/php-fcgi server.<\/p>\n<p>location ~ \\.php$ {<\/p>\n<p># Zero-day exploit defense.<\/p>\n<p># http:\/\/forum.nginx.org\/read.php?2,88845,page=3<\/p>\n<p># Won\u2019t work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm\/php-fcgi.<\/p>\n<p># Comment the \u2018try_files\u2019 line out if you set up php-fpm\/php-fcgi on another machine. And then cross your fingers that you won\u2019t get hacked.<\/p>\n<p>try_files $uri =404;<\/p>\n<p>fastcgi_split_path_info ^(.+\\.php)(\/.+)$;<\/p>\n<p>#NOTE: You should have \u201ccgi.fix_pathinfo = 0;\u201d in php.ini<\/p>\n<p>include fastcgi_params;<\/p>\n<p>fastcgi_index index.php;<\/p>\n<p>fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<\/p>\n<p># fastcgi_intercept_errors on;<\/p>\n<p>fastcgi_pass php;<\/p>\n<p>}<\/p><\/blockquote>\n<p>\uff15\u3001\u591a\u535a\u5ba2\u5b50\u76ee\u5f55\u3001\u591a\u535a\u5ba2\u5b50\u57df\u540d\u901a\u7528<\/p>\n<blockquote><p># WordPress multisite subdirectory rules.<\/p>\n<p># Designed to be included in any server {} block.<\/p>\n<p># This order might seem weird \u2013 this is attempted to match last if rules below fail.<\/p>\n<p># http:\/\/wiki.nginx.org\/HttpCoreModule<\/p>\n<p>location \/ {<\/p>\n<p>try_files $uri $uri\/ \/index.php?$args;<\/p>\n<p>}<\/p>\n<p># Add trailing slash to *\/wp-admin requests.<\/p>\n<p>rewrite \/wp-admin$ $scheme:\/\/$host$uri\/ permanent;<\/p>\n<p># Directives to send expires headers and turn off 404 error logging.<\/p>\n<p>location ~* \\.(js|css|png|jpg|jpeg|gif|ico)$ {<\/p>\n<p>expires 24h;<\/p>\n<p>log_not_found off;<\/p>\n<p>}<\/p>\n<p># Pass uploaded files to wp-includes\/ms-files.php.<\/p>\n<p>rewrite \/files\/$ \/index.php last;<\/p>\n<p># For multisite: Use a caching plugin that creates symlinks to the correct subdirectory structure to get some performance gains.<\/p>\n<p>set $cachetest \u201c$document_root\/wp-content\/ms-filemap\/${host}${uri}\u201d;<\/p>\n<p>if ($uri ~ \/$) {<\/p>\n<p>set $cachetest \u201c\u201d;<\/p>\n<p>}<\/p>\n<p>if (-f $cachetest) {<\/p>\n<p># Rewrites the URI and stops rewrite processing so it doesn\u2019t start over and attempt to pass it to the next rule.<\/p>\n<p>rewrite ^ \/wp-content\/ms-filemap\/${host}${uri} break;<\/p>\n<p>}<\/p>\n<p>if ($uri !~ wp-content\/plugins) {<\/p>\n<p>rewrite \/files\/(.+)$ \/wp-includes\/ms-files.php?file=$1 last;<\/p>\n<p>}<\/p>\n<p># Uncomment one of the lines below for the appropriate caching plugin (if used).<\/p>\n<p>#include global\/wordpress-ms-subdir-wp-super-cache.conf;<\/p>\n<p>#include global\/wordpress-ms-subdir-w3-total-cache.conf;<\/p>\n<p># Rewrite multisite \u2018\u2026\/wp-.*\u2019 and \u2018\u2026\/*.php\u2019.<\/p>\n<p>if (!-e $request_filename) {<\/p>\n<p>rewrite ^\/[_0-9a-zA-Z-]+(\/wp-.*) $1 last;<\/p>\n<p>rewrite ^\/[_0-9a-zA-Z-]+.*(\/wp-admin\/.*\\.php)$ $1 last;<\/p>\n<p>rewrite ^\/[_0-9a-zA-Z-]+(\/.*\\.php)$ $1 last;<\/p>\n<p>}<\/p>\n<p># Pass all .php files onto a php-fpm\/php-fcgi server.<\/p>\n<p>location ~ \\.php$ {<\/p>\n<p># Zero-day exploit defense.<\/p>\n<p># http:\/\/forum.nginx.org\/read.php?2,88845,page=3<\/p>\n<p># Won\u2019t work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm\/php-fcgi.<\/p>\n<p># Comment the \u2018try_files\u2019 line out if you set up php-fpm\/php-fcgi on another machine. And then cross your fingers that you won\u2019t get hacked.<\/p>\n<p>try_files $uri =404;<\/p>\n<p>fastcgi_split_path_info ^(.+\\.php)(\/.+)$;<\/p>\n<p>#NOTE: You should have \u201ccgi.fix_pathinfo = 0;\u201d in php.ini<\/p>\n<p>include fastcgi_params;<\/p>\n<p>fastcgi_index index.php;<\/p>\n<p>fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<\/p>\n<p># fastcgi_intercept_errors on;<\/p>\n<p>fastcgi_pass php;<\/p>\n<p>}<\/p><\/blockquote>\n<p>\u66f4\u8be6\u7ec6\u7684\u8bf4\u660e\u8bf7\u53c2\u8003 : <a href=\"http:\/\/codex.wordpress.org\/Nginx\">http:\/\/codex.wordpress.org\/Nginx<\/a><\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013\u5206\u9694\u7ebf\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<\/p>\n<p>WordPress \u81ea\u5b9a\u4e49 Permalink\u00a0\u5c0f\u6280\u5de7\uff1a<\/p>\n<p>WordPress Permalink \u4e2d\u5220\u9664 index.php \u4e2d\u65b9\u6cd5\uff1a<\/p>\n<p>a: Apache \u670d\u52a1\u5668\uff0c\u7167\u7740 WordPress \u5b98\u65b9\u63d0\u4f9b\u7684 .htaccess \u6587\u4ef6\u4e0a\u4f20\u5c31\u80fd\u89e3\u51b3\u95ee\u9898<\/p>\n<p>b:Nginx \u670d\u52a1\u5668<\/p>\n<p>\u4ee5\u4e0a\u7684\u914d\u7f6e\u6587\u4ef6\u5c31\u4e0d\u4e00\u5b9a\u80fd\u89e3\u51b3\u95ee\u9898\uff0c<\/p>\n<p>\u5bf9\u4e8e\u5355\u535a\u5ba2 WordPress \uff1a\u627e\u5230 location \/ \u533a\u5757\uff1a<\/p>\n<p>\u52a0\u5165\u6216\u4fee\u6539\u4e3a\u4ee5\u4e0b\u4ee3\u7801<\/p>\n<blockquote><p>try_files $uri $uri\/ \/index.php?q=$uri&amp;$args;<\/p><\/blockquote>\n<p>\u5bf9\u4e8e\u591a\u535a\u5ba2 WordPress \uff1a\u65b0\u589e location \/blog\/ \u533a\u5757\uff1a<\/p>\n<blockquote><p>location \/blog\/ {<br \/>\ntry_files $uri $uri\/ \/blog\/index.php?q=$uri&amp;$args;<br \/>\n}<\/p><\/blockquote>\n<p>\u6700\u540e\u522b\u5fd8\u8bb0\u4e86<\/p>\n<blockquote><p>nginx -s reload<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>WordPress + Nginx \u914d\u7f6e\u65b9\u6cd5\uff1ahttps:\/\/codex.wor&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/freesilo.com\/?p=156\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,10],"tags":[],"class_list":["post-156","post","type-post","status-publish","format-standard","hentry","category-nginx","category-wordpress"],"_links":{"self":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=156"}],"version-history":[{"count":3,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}