{"id":127,"date":"2024-02-24T16:53:49","date_gmt":"2024-02-24T08:53:49","guid":{"rendered":"http:\/\/esufy.com\/?p=127"},"modified":"2024-02-24T16:53:49","modified_gmt":"2024-02-24T08:53:49","slug":"nginx%e5%b8%b8%e7%94%a8%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/www.190130.xyz\/?p=127","title":{"rendered":"Nginx\u5e38\u7528\u914d\u7f6e"},"content":{"rendered":"<p>\u4e00\u3001\u57fa\u7840\u914d\u7f6e<\/p>\n<div class=\"cnblogs_code\">\n<pre>user                            root;\nworker_processes                1;\n\nevents {\n  worker_connections            10240;\n}\n\nhttp {\n  log_format                    '$remote_addr - $remote_user [$time_local] ' '\"$request\" $status $body_bytes_sent ' '\"$http_referer\" \"$http_user_agent\"';\n  include                       mime.types;\n  default_type                  application\/octet-stream;\n  sendfile                      on;\n  #autoindex                    on;\n  #autoindex_exact_size         off;\n  autoindex_localtime           on;\n  keepalive_timeout             65;\n  gzip                          on;\n  gzip_disable                  \"msie6\";\n  gzip_min_length               100;\n  gzip_buffers                  4 16k;\n  gzip_comp_level               1;\n  gzip_types                  text\/plain application\/x-javascript text\/css application\/xml text\/javascript application\/x-httpd-php image\/jpeg image\/gif image\/png;\n  gzip_types                    \"*\";\n  gzip_vary                     off;\n  server_tokens                 off;\n  client_max_body_size          200m;\n\n  server {\n    listen                      80 default_server;\n    server_name                 _;\n    return                      403 \/www\/403\/index.html;\n  }\n\n  include                       ..\/serve\/*.conf;\n}<\/pre>\n<\/div>\n<p>\u4e8c\u3001\u9690\u85cf Nginx \u7248\u672c\u4fe1\u606f<\/p>\n<div class=\"cnblogs_code\">\n<pre>http {\n  server_tokens         off;\n}<\/pre>\n<\/div>\n<p>\u4e09\u3001\u7981\u6b62ip\u76f4\u63a5\u8bbf\u95ee80\u7aef\u53e3<\/p>\n<div class=\"cnblogs_code\">\n<pre>server {\n  listen                80 default;\n  server_name           _;\n  return                500;\n}<\/pre>\n<\/div>\n<p>\u56db\u3001\u542f\u52a8 web \u670d\u52a1 (vue \u9879\u76ee\u4e3a\u4f8b)<\/p>\n<div class=\"cnblogs_code\">\n<pre>server {\n  # \u9879\u76ee\u542f\u52a8\u7aef\u53e3\n  listen            80;\n  # \u57df\u540d\uff08localhost\uff09\n  server_name       _;\n  # \u7981\u6b62 iframe \u5d4c\u5957\n  add_header        X-Frame-Options SAMEORIGIN;\n  \n  # \u8bbf\u95ee\u5730\u5740 \u6839\u8def\u5f84\u914d\u7f6e\n  location \/ {\n    # \u9879\u76ee\u76ee\u5f55\n    root        html;\n    # \u9ed8\u8ba4\u8bfb\u53d6\u6587\u4ef6\n    index           index.html;\n    # \u914d\u7f6e history \u6a21\u5f0f\u7684\u5237\u65b0\u7a7a\u767d\n    try_files       $uri $uri\/ \/index.html;\n  }\n  \n  # \u540e\u7f00\u5339\u914d\uff0c\u89e3\u51b3\u9759\u6001\u8d44\u6e90\u627e\u4e0d\u5230\u95ee\u9898\n  location ~* \\.(gif|jpg|jpeg|png|css|js|ico)$ { \n    root           html\/static\/;\n  }\n  \n  # \u56fe\u7247\u9632\u76d7\u94fe\n  location ~\/static\/.*\\.(jpg|jpeg|png|gif|webp)$ {\n    root              html;\n    valid_referers    *.deeruby.com;\n    if ($invalid_referer) {\n      return          403;\n    }\n  }\n  \n  # \u8bbf\u95ee\u9650\u5236\n  location \/static {\n    root               html;\n    # allow \u5141\u8bb8\n    allow              39.xxx.xxx.xxx;\n    # deny  \u62d2\u7edd\n    deny               all;\n  }\n}<\/pre>\n<\/div>\n<p>\u4e94\u3001PC\u7aef\u548c\u79fb\u52a8\u7aef\u4f7f\u7528\u4e0d\u540c\u7684\u9879\u76ee\u6587\u4ef6\u6620\u5c04<\/p>\n<div class=\"cnblogs_code\">\n<pre>server {\n  ......\n  location \/ {\n    root \/home\/static\/pc;\n    if ($http_user_agent ~* '(mobile|android|iphone|ipad|phone)') {\n      root \/home\/static\/mobile;\n    }\n    index index.html;\n  }\n}<\/pre>\n<\/div>\n<p>\u516d\u3001\u4e00\u4e2aweb\u670d\u52a1\uff0c\u914d\u7f6e\u591a\u4e2a\u9879\u76ee (location \u5339\u914d\u8def\u7531\u533a\u522b)<\/p>\n<div class=\"cnblogs_code\">\n<pre>server {\n  listen                80;\n  server_name           _;\n  \n  # \u4e3b\u5e94\u7528\n  location \/ {\n    root                html\/main;\n    index               index.html;\n    try_files           $uri $uri\/ \/index.html;\n  }\n  \n  # \u5b50\u5e94\u7528\u4e00\n  location ^~ \/store\/ {\n    proxy_pass          http:\/\/localhost:8001;\n    proxy_redirect      off;\n    proxy_set_header    Host $host;\n    proxy_set_header    X-Real-IP $remote_addr;\n    proxy_set_header    X-Forwarded-For\n    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;\n  }\n  \n  # \u5b50\u5e94\u7528\u4e8c\n  location ^~ \/school\/ {\n    proxy_pass          http:\/\/localhost:8002;\n    proxy_redirect      off;\n    proxy_set_header    Host $host;\n    proxy_set_header    X-Real-IP $remote_addr;\n    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;\n  }\n  \n  # \u9759\u6001\u8d44\u6e90\u8bfb\u53d6\u4e0d\u5230\u95ee\u9898\u5904\u7406\n  rewrite ^\/api\/profile\/(.*)$ \/(\u66ff\u6362\u6210\u6b63\u786e\u8def\u5f84\u7684\u6587\u4ef6\u7684\u4e0a\u4e00\u5c42\u76ee\u5f55)\/$1 last;\n}\n\n# \u5b50\u5e94\u7528\u4e00\u670d\u52a1\nserver {\n  listen                8001;\n  server_name           _;\n  location \/ {\n    root                html\/store;\n    index               index.html;\n    try_files           $uri $uri\/ \/index.html;\n  }\n  \n  location ^~ \/store\/ {\n    alias               html\/store\/;\n    index               index.html index.htm;\n    try_files           $uri \/store\/index.html;\n  }\n  \n  # \u63a5\u53e3\u4ee3\u7406\n  location  \/api {\n    proxy_pass          http:\/\/localhost:8089;\n  }\n}\n\n# \u5b50\u5e94\u7528\u4e8c\u670d\u52a1\nserver {\n  listen                8002;\n  server_name           _;\n  location \/ {\n    root                html\/school;\n    index               index.html;\n    try_files           $uri $uri\/ \/index.html;\n  }\n  \n  location ^~ \/school\/ {\n    alias               html\/school\/;\n    index               index.html index.htm;\n    try_files           $uri \/school\/index.html;\n  }\n  \n  # \u63a5\u53e3\u4ee3\u7406\n  location  \/api {\n    proxy_pass          http:\/\/localhost:10010;\n  }\n}<\/pre>\n<\/div>\n<p>\u4e03\u3001\u914d\u7f6e\u8d1f\u8f7d\u5747\u8861<\/p>\n<div class=\"cnblogs_code\">\n<pre>upstream my_upstream {\n  server                http:\/\/localhost:9001;\n  server                http:\/\/localhost:9002;\n  server                http:\/\/localhost:9003;\n}\n\nserver {\n  listen                9000;\n  server_name           test.com;\n\n  location \/ {\n    proxy_pass          my_upstream;\n    proxy_set_header    Host $proxy_host;\n    proxy_set_header    X-Real-IP $remote_addr;\n    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;\n  }\n}<\/pre>\n<\/div>\n<p>\u516b\u3001SSL \u914d\u7f6e HTTPS<\/p>\n<div class=\"cnblogs_code\">\n<pre>server {\n  listen                      80;\n  server_name                 www.xxx.com;\n  # \u5c06 http \u91cd\u5b9a\u5411\u8f6c\u79fb\u5230 https\n  return 301 https:\/\/$server_name$request_uri;\n}\n\nserver {\n  listen                      443 ssl;\n  server_name                 www.xxx.com;\n  ssl_certificate             \/etc\/nginx\/ssl\/www.xxx.com.pem;\n  ssl_certificate_key         \/etc\/nginx\/ssl\/www.xxx.com.key;\n  ssl_session_timeout         10m;\n  ssl_ciphers                 ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;\n  ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;\n  ssl_prefer_server_ciphers   on;\n  \n  location \/ {\n    root                    \/project\/xxx;\n    index                   index.html index.htm index.md;\n    try_files               $uri $uri\/ \/index.html;\n  }\n}<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u57fa\u7840\u914d\u7f6e user root; worker_processes 1; events { worker_c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-nginx"],"_links":{"self":[{"href":"https:\/\/www.190130.xyz\/index.php?rest_route=\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.190130.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.190130.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.190130.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.190130.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=127"}],"version-history":[{"count":0,"href":"https:\/\/www.190130.xyz\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.190130.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.190130.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.190130.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}