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