{"id":139201,"date":"2019-03-19T08:33:02","date_gmt":"2019-03-19T00:33:02","guid":{"rendered":"https:\/\/gulass.cn\/?p=139201"},"modified":"2019-03-08T09:33:52","modified_gmt":"2019-03-08T01:33:52","slug":"nginx-base-config","status":"publish","type":"post","link":"https:\/\/gulass.cn\/nginx-base-config.html","title":{"rendered":"\u5927\u795e\u6559\u4f60Nginx\u5e38\u7528\u57fa\u7840\u914d\u7f6e\u65b9\u6848"},"content":{"rendered":"
Nginx\u7684fastcgi\u6a21\u5757\u53c2\u6570\u8bbe\u7f6e<\/strong><\/span><\/div>\n

Nginx \u6709\u4e24\u4e2a\u914d\u7f6e\u6587\u4ef6fastcgi_params\u3001fastcgi.conf\uff0c\u4e24\u8005\u552f\u4e00\u7684\u533a\u522b\u662f\uff0cfastcgi.conf \u591a\u4e00\u4e2a\u53c2\u6570 SCRIPT_FILENAME\uff0cdiff\u663e\u793a\u5982\u4e0b:<\/p>\n

\r\n$diff fastcgi fastcgi_params\r\n\r\n< fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\r\nvim \u8fdb\u5165\/usr\/local\/nginx\/conf\/fastcgi_params\u6587\u4ef6\r\n\r\n#\u8bf7\u6c42\u7684\u53c2\u6570;\u5982?app=123fastcgi_param\r\nfastcgi_param QUERY_STRING $query_string;\r\n\r\n##\u8bf7\u6c42\u7684\u52a8\u4f5c(GET,POST)\r\nfastcgi_param REQUEST_METHOD $request_method;\r\n\r\n#\u8bf7\u6c42\u5934\u4e2d\u7684Content-Type\u5b57\u6bb5\r\nfastcgi_param CONTENT_TYPE $content_type;\r\n\r\n#\u8bf7\u6c42\u5934\u4e2d\u7684Content-length\u5b57\u6bb5\r\nfastcgi_param CONTENT_LENGTH $content_length;\r\n\r\n#\u811a\u672c\u540d\u79f0\r\nfastcgi_param SCRIPT_NAME $fastcgi_script_name;\r\n\r\n#\u8bf7\u6c42\u7684\u5730\u5740\u4e0d\u5e26\u53c2\u6570\r\nfastcgi_param REQUEST_URI $request_uri;\r\n\r\n#\u4e0e$uri\u76f8\u540c\r\nfastcgi_param DOCUMENT_URI $document_uri;\r\n\r\n#\u7f51\u7ad9\u7684\u6839\u76ee\u5f55\u3002\u5728server\u914d\u7f6e\u4e2droot\u6307\u4ee4\u4e2d\u6307\u5b9a\u7684\u503c\r\nfastcgi_param DOCUMENT_ROOT $document_root;\r\n\r\n#\u8bf7\u6c42\u4f7f\u7528\u7684\u534f\u8bae\uff0c\u901a\u5e38\u662fHTTP\/1.0\u6216HTTP\/1.1\r\nfastcgi_param SERVER_PROTOCOL $server_protocol;\r\n\r\n#https \u5982\u679cvalue\u975e\u7a7a\u624d\u8fdb\u884c\u8bbe\u7f6e\r\nfastcgi_param HTTPS $https if_not_empty;\r\n\r\n#cgi \u7248\u672c\r\nfastcgi_param GATEWAY_INTERFACE CGI\/1.1;\r\n\r\n#nginx \u7248\u672c\u53f7\uff0c\u53ef\u4fee\u6539\u3001\u9690\u85cf\r\nfastcgi_param SERVER_SOFTWARE nginx\/$nginx_version;\r\n\r\n#\u5ba2\u6237\u7aefIP\r\nfastcgi_param REMOTE_ADDR $remote_addr;\r\n\r\n#\u5ba2\u6237\u7aef\u7aef\u53e3\r\nfastcgi_param REMOTE_PORT $remote_port;\r\n\r\n#\u670d\u52a1\u5668IP\u5730\u5740\r\nfastcgi_param SERVER_ADDR $server_addr;\r\n\r\n#\u670d\u52a1\u5668\u7aef\u53e3\r\nfastcgi_param SERVER_PORT $server_port;\r\n\r\n#\u670d\u52a1\u5668\u540d\uff0c\u57df\u540d\u5728server\u914d\u7f6e\u4e2d\u6307\u5b9a\u7684server_name\r\nfastcgi_param SERVER_NAME $server_name;\r\n\r\n\u53ef\u81ea\u5b9a\u4e49\u53d8\u91cf\r\nfastcgi_param PATH_INFO $path_info;\r\n\r\n#\u5728\u5c3e\u90e8\u53e6\u8d77\u4e00\u884c\u8ffd\u52a0\u5373\u53ef\u4fdd\u5b58\u8ddffastcgi.conf \u4e00\u81f4\r\nfastcgi_param REDIRECT_STATUS 200;\r\nfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\r\n\r\n\u5728php\u53ef\u6253\u5370\u51fa\u4e0a\u9762\u7684\u670d\u52a1\u73af\u5883\u53d8\u91cf\r\n\u5982\uff1aecho $_SERVER[\u2018REMOTE_ADDR\u2019]\r\n<\/pre>\n
Nginx\u7684\u5e38\u7528\u6307\u4ee4\u89e3\u91ca<\/strong><\/span><\/div>\n
\r\nfastcgi_pass\r\n\r\n\u8fd9\u4e2a\u547d\u4ee4\u662f\u6307\u5b9a\u5c06http\u4ee3\u7406\u5230\u54ea\u4e2afastcgi\u670d\u52a1\u7aef\u63a5\u53e3\u3002fastcgi_pass\u540e\u9762\u662f\u586b\u5199fastcgi\u670d\u52a1\u7aef\u5730\u5740\u7684\uff0c\u8fd9\u4e2a\u5730\u5740\u53ef\u4ee5\u662f\u57df\u5730\u5740\uff0c\u4e5f\u53ef\u4ee5\u662fUninx-\u57df\u5957\u63a5\u5b57, \u53e6\u5916\u4e5f\u53ef\u4ee5\u662fupstream\u4e2d\u8bbe\u7f6e\u7684\u53cd\u5411\u4ee3\u7406\u3002\r\n\r\nfastcgi_pass localhost:9000; #\u9ed8\u8ba4PHP\u8d77\u57289000\u7aef\u53e3\r\nfastcgi_pass unix:\/tmp\/fastcgi.socket;\r\nfastcgi_pass upstream_php5; #\u8fd9\u91cc\u6307\u5b9a\u7684\u53cd\u5411\u4ee3\u7406\u53ef\u4ee5\u5728nginx.conf\u4e2dupstream\u4e2d\u8bbe\u7f6e\r\nfastcgi_param\r\n\r\n\u8fd9\u4e2a\u547d\u4ee4\u662f\u8bbe\u7f6efastcgi\u8bf7\u6c42\u4e2d\u7684\u53c2\u6570\uff0c\u9ed8\u8ba4\u53c2\u6570\u5728\u4e0a\u9762\u63d0\u5230\u7684fastcgi\u6a21\u5757\u53c2\u6570\u6587\u4ef6\u4e2d\uff0c\u5177\u4f53\u8bbe\u7f6e\u7684\u4e1c\u897f\u53ef\u4ee5\u5728$_SERVER\u4e2d\u83b7\u53d6\u5230\u3002\r\n\u6bd4\u5982\u4f60\u60f3\u8981\u8bbe\u7f6e\u5f53\u524d\u7684\u673a\u5668\u73af\u5883\uff0c\u53ef\u4ee5\u4f7f\u7528fastcgi_param ENV test;\u6765\u8bbe\u7f6e\u3002\r\n\u5bf9\u4e8ephp\u6765\u8bf4\uff0c\u6700\u5c11\u9700\u8981\u8bbe\u7f6e\u7684\u53d8\u91cf\u6709\uff1a\r\n\r\nfastcgi_param SCRIPT_FILENAME \/home\/www\/scripts\/php$fastcgi_script_name;\r\nfastcgi_param QUERY_STRING $query_string;\r\nfastcgi_index\r\n\r\n\u8fd9\u4e2a\u547d\u4ee4\u8bbe\u7f6e\u4e86fastcgi\u9ed8\u8ba4\u4f7f\u7528\u7684\u811a\u672c\u3002\u5c31\u662f\u5f53SCRIPT_FILENAME\u6ca1\u6709\u547d\u4e2d\u811a\u672c\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u7684\u5c31\u662ffastcgi_index\u8bbe\u7f6e\u7684\u811a\u672c\u3002\r\n\r\nfastcgi_index index.php;\r\n<\/pre>\n

\u4ee5\u4e0a\u4e09\u4e2a\u547d\u4ee4\u80fd\u7ec4\u6210\u6700\u57fa\u672c\u7684fastcgi\u8bbe\u7f6e\u4e86:<\/p>\n

\r\nlocation \/ {\r\nfastcgi_pass localhost:9000;\r\nfastcgi_index index.php;\r\n\r\n#\u4e0b\u9762\u8fd9\u4e00\u4e2a\u53ef\u4ee5\u76f4\u63a5\u5728fastcgi_param\u914d\u7f6e\u6587\u4ef6\u4e2d\u6307\u5b9a\r\nfastcgi_param SCRIPT_FILENAME \/home\/www\/scripts\/php$fastcgi_script_name;\r\nfastcgi_param QUERY_STRING $query_string;\r\nfastcgi_param REQUEST_METHOD $request_method;\r\nfastcgi_param CONTENT_TYPE $content_type;\r\nfastcgi_param CONTENT_LENGTH $content_length;\r\n}\r\n<\/pre>\n
\u56fe\u7247(\u6216\u8005\u9759\u6001\u6587\u4ef6)\u670d\u52a1\u5668\u914d\u7f6e<\/strong><\/span><\/div>\n
\r\nserver {\r\nlisten 80;\r\nserver_name images.xxx.com img.movie.xxx.com;\r\nroot \/data\/vhosts\/xxx.com\/images\/public_html\/;\r\nindex index.shtml index.html index.htm;\r\n\r\n#\u5982\u679c\u662fjs\u3001css\u3001json\u6587\u4ef6\u53ef\u4ee5\u6307\u5b9a\u538b\u7f29\u6765\u51cf\u5c11\u4f20\u8f93\u6587\u4ef6\u5927\u5c0f\r\ngzip_types text\/plain application\/x-javascript text\/css application\/xml text\/xml application\/json;\r\n}\r\n<\/pre>\n
\u57fa\u7840\u670d\u52a1\u5668<\/strong><\/span><\/div>\n
\r\nserver {\r\nlisten 80;\r\nserver_name www.xxx.com;\r\nroot \/data\/vhosts\/xxxx.com\/public_html\/;\r\nindex index.htm index.php index.html index.shtml;\r\n\r\nlocation \/ {\r\nssi on;\r\nssi_silent_errors on;\r\nssi_types text\/shtml;\r\ninclude other.conf; #\u8fd9\u91cc\u53ef\u4ee5\u914d\u7f6e\u5176\u4ed6\u7684\u516c\u5171\u914d\u7f6e\uff0c\u6216\u8005\u91cd\u5199\u89c4\u5219\r\n}\r\n\r\nlocation ~\\.php$ {\r\nexpires off;\r\ninclude fastcgi_params; #fastcgi \u6307\u5b9a\u7684\u53c2\u6570\u914d\u7f6e\r\nfastcgi_pass 127.0.0.1:9000; #\u8fd9\u91cc\u540c\u4e0a\u4e5f\u53ef\u6307\u5b9a\u4ee3\u7406\u6216socket\r\nfastcgi_index index.php;\r\nfastcgi_connect_timeout 300;\r\nfastcgi_send_timeout 300;\r\nfastcgi_read_timeout 300;\r\n}\r\n<\/pre>\n
\u914d\u7f6essi_inclue\u8bbf\u95ee\u7684\u76ee\u5f55\u4e0d\u5b58\u5728\u662f\u6307\u5b9a\u7684\u76ee\u5f55<\/strong><\/span><\/div>\n
\r\nlocation ~ \/ssi_include\/ {\r\nif (!-e $request_filename) {\r\nrewrite ^(.*)$ \/blank.html last;\r\n}\r\n}\r\n<\/pre>\n
\u914d\u7f6e\u9759\u6001\u6587\u4ef6\u9ed8\u8ba4\u7684\u9519\u8bef\u9875\u9762<\/strong><\/span><\/div>\n
\r\nlocation ~(\\.html|\\.htm|\\.shtml)$ {\r\nerror_page 404 500 502 503 504 \/404.html;\r\n}\r\n}\r\n<\/pre>\n
Auth\u6743\u9650\u8bbe\u7f6e<\/strong><\/span><\/div>\n

step 1. \u5728\u6839\u57df\u540d\u4e0b\u9762\u9700\u8981\u914d\u7f6e\u6743\u9650\u7684\u76ee\u5f55\u8bbe\u7f6elocation<\/p>\n

\r\nlocation \/phpMyAdmin\/ {\r\nallow 192.168.0.1;\r\nallow xx.xx.xxx.xxx;\r\nallow xx.xx.xxx.xxx;\r\ndeny all;\r\nauth_basic \"Restricted\";\r\nauth_basic_user_file \/usr\/local\/nginx\/conf\/auth_phpmyadmin.pass;\r\n}\r\n<\/pre>\n

step2. \u5728 auth_basic_user_file \u6307\u5b9a\u7684\u6587\u4ef6\u4e0b\u9762\u589e\u52a0\u8d26\u53f7\u5bc6\u7801,\u4e00\u884c\u4e00\u4e2a<\/p>\n

\r\nusername1:password1\r\nusername2:password2\r\nusername3:password3\r\nusername4:password4\r\n<\/pre>\n
Nginx\u53cd\u5411\u4ee3\u7406<\/strong><\/span><\/div>\n

\u7b2c\u4e00\u79cd\u53cd\u5411\u4ee3\u7406:<\/p>\n

\r\nlocation \/ {\r\nproxy_pass http:\/\/192.168.1.4:8099\/;\r\n#\u82e5\u9488\u5bf9\u4e0d\u540c\u7684\u76ee\u5f55\u8fdb\u884c\u4ee3\u7406\u628a\u4e0b\u9762\u7684\u914d\u7f6e\u653e\u5230\u6839\u76ee\u5f55\u4ee3\u7406\u7684\u4e0a\u9762\r\n#proxy_pass http:\/\/192.168.1.4:8099\/linuxtone\/;\r\nproxy_redirect default ;\r\n}\r\n<\/pre>\n

\u7b2c\u4e8c\u79cd\u53cd\u5411\u4ee3\u7406:<\/p>\n

\r\nupstream\u914d\u7f6e\r\nupstream xx.xxx.com {\r\nserver 192.168.1.4:8099;\r\n}\r\n<\/pre>\n
\u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6<\/strong><\/span><\/div>\n
\r\nserver\r\n{\r\nlisten 80;\r\nserver_name bbs.linuxtone.conf;\r\nindex index.html index.htm;\r\nroot \/date\/vhosts\/xxx.com\/;\r\n\r\nlocation ~ ^\/NginxStatus\/ {\r\nstub_status on;\r\naccess_log off;\r\n}\r\n<\/pre>\n
\r\nlocation \/ {\r\nproxy_redirect off ;\r\nproxy_set_header Host $host;\r\nproxy_set_header X-Real-IP $remote_addr;\r\nproxy_set_header REMOTE-HOST $remote_addr;\r\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\nclient_max_body_size 50m; #\u7f13\u51b2\u533a\u4ee3\u7406\u7f13\u51b2\u7528\u6237\u7aef\u8bf7\u6c42\u7684\u6700\u5927\u5b57\u8282\u6570,\u53ef\u4ee5\u7406\u89e3\u4e3a\u4fdd\u5b58\u5230\u672c\u5730\u518d\u4f20\u7ed9\u7528\u6237\r\nclient_body_buffer_size 256k;\r\nproxy_connect_timeout 30; #nginx\u8ddf\u540e\u7aef\u670d\u52a1\u5668\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4(\u4ee3\u7406\u8fde\u63a5\u8d85\u65f6)\r\nproxy_send_timeout 30;\r\nproxy_read_timeout 60; #\u8fde\u63a5\u6210\u529f\u540e\uff0c\u540e\u7aef\u670d\u52a1\u5668\u54cd\u5e94\u65f6\u95f4(\u4ee3\u7406\u63a5\u6536\u8d85\u65f6)\r\nproxy_buffer_size 256k; #\u8bbe\u7f6e\u4ee3\u7406\u670d\u52a1\u5668\uff08nginx\uff09\u4fdd\u5b58\u7528\u6237\u5934\u4fe1\u606f\u7684\u7f13\u51b2\u533a\u5927\u5c0f\r\nproxy_buffers 4 256k; #proxy_buffers\u7f13\u51b2\u533a\uff0c\u7f51\u9875\u5e73\u5747\u5728256k\u4ee5\u4e0b\u7684\u8bdd\uff0c\u8fd9\u6837\u8bbe\u7f6e\r\nproxy_busy_buffers_size 256k;\r\nproxy_temp_file_write_size 256k;\r\nproxy_next_upstream error timeout invalid_header http_500 http_503 http_404;\r\nproxy_max_temp_file_size 128m;\r\nproxy_ignore_client_abort on; #\u4e0d\u5141\u8bb8\u4ee3\u7406\u7aef\u4e3b\u52a8\u5173\u95ed\u8fde\u63a5\r\n#http:\/\/xx.xxx.com \u6307\u4e0a\u9762upstream\u5757\u7684\u540d\u79f0\r\nproxy_pass http:\/\/xx.xxx.com;\r\n}\r\n<\/pre>\n
Apache\u53cd\u5411\u4ee3\u7406<\/strong><\/span><\/div>\n

#\u8bbe\u7f6e\u8be5\u57df\u540d\u8f6c\u53d1\u7ed98080\u7aef\u53e3<\/p>\n

\r\nServerAdmin webmaster@dummy-host2.example.com\r\nServerName www.xxx.com\r\nProxyRequests off\r\nOrder deny,allow\r\nAllow from all\r\nProxyPass \/ http:\/\/www.xxx.com:8080\/\r\nProxyPassReverse \/ http:\/\/www.xxx.com:8080\/\r\n<\/pre>\n

ProxyPassReverse\u4e00\u822c\u548cProxyPass\u6307\u4ee4\u914d\u5408\u4f7f\u7528\uff0c\u6b64\u6307\u4ee4\u4f7fApache\u8c03\u6574HTTP\u91cd\u5b9a\u5411\u5e94\u7b54\u4e2dLocation, Content-Location, URI\u5934\u91cc\u7684URL\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5728Apache\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u4f7f\u7528\u65f6\uff0c\u3002\u540e\u7aef\u670d\u52a1\u5668\u7684HTTP\u91cd\u5b9a\u5411\u9020\u6210\u7684\u7ed5\u8fc7\u53cd\u5411\u4ee3\u7406\u7684\u95ee\u9898<\/p>\n

\u7981\u6b62\u8718\u86db\u8bbf\u95ee<\/strong><\/span><\/div>\n

#\u5224\u65adUA\uff0c\u5982\u679cUA\u4e0d\u5305\u542bspider\u6216\u8005bot\uff08\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff09,\u8868\u793aUA\u4e3a\u6b63\u5e38\u7528\u6237<\/p>\n

#\u8bbe\u7f6e\u53d8\u91cfis_human\u503c\u4e3ayes<\/p>\n

\r\nif ($http_user_agent !~* \"spider|bot\") {\r\nset $is_human 'yes';\r\n}\r\n<\/pre>\n

#\u5f53\u6709\u4efb\u610f\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u8be5UA\u4e0d\u662f\u6b63\u5e38\u7528\u6237\uff0c\u5219\u8868\u793a\u5e94\u8be5\u662f\u8718\u86db\u7c7b\u7a0b\u5e8f\uff0c\u5219\u8fd4\u56de403<\/p>\n

\r\nlocation \/ {\r\nif ($is_human != 'yes') {\r\nreturn 403;\r\n}\r\n}\r\n<\/pre>\n

#\u5f53\u6709\u4efb\u610f\u8bf7\u6c42\u7684\u65f6\u5019<\/p>\n

\r\nlocation \/ {\r\n#\u5f53\u8bbf\u95ee\u8005UA\u5305\u542b\u6709spider\u6216\u5219bot\u7684\u65f6\u5019\uff08\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff09\uff0c\u8bf4\u660e\u662f\u8718\u86db\u7c7b\u6765\u8bbf\r\nif ($http_user_agent ~* \"spider|bot\") {\r\n# \u76f4\u63a5\u5c31\u5c4f\u853d\u8718\u86db\u7684\u6574\u7ad9\u8bbf\u95ee\r\nreturn 403;\r\n}\r\n}\r\n<\/pre>\n

\u7ed9\u7cfb\u7edf\u6dfb\u52a0\u4e86robots.txt\u6587\u4ef6:<\/p>\n

\r\nUser-agent: *\r\nDisallow: \/\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Nginx \u6709\u4e24\u4e2a\u914d\u7f6e\u6587\u4ef6fastcgi_params\u3001fastcgi.conf\uff0c\u4e24\u8005\u552f\u4e00\u7684\u533a\u522b\u662f\uff0cfastc […]<\/p>\n","protected":false},"author":668,"featured_media":94293,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-139201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/139201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=139201"}],"version-history":[{"count":5,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/139201\/revisions"}],"predecessor-version":[{"id":139273,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/139201\/revisions\/139273"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/94293"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=139201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=139201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=139201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}