{"id":77117,"date":"2024-03-08T15:53:32","date_gmt":"2024-03-08T07:53:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=77117"},"modified":"2024-03-08T15:53:32","modified_gmt":"2024-03-08T07:53:32","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-nginx-%e9%85%8d%e7%bd%ae%e9%80%8f%e6%98%8e-http-%e5%92%8c-https-%e4%bb%a3%e7%90%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/77117.html","title":{"rendered":"\u5982\u4f55\u7528 Nginx \u914d\u7f6e\u900f\u660e HTTP \u548c HTTPS \u4ee3\u7406"},"content":{"rendered":"<p><strong>\u5728\u914d\u7f6e Nginx \u4ee5\u7528\u4f5c\u900f\u660eHTTP\u548cHTTPS\u4ee3\u7406\u65f6\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u786e\u5b9a\u4ee3\u7406\u9700\u6c42\u3001\u5b89\u88c5Nginx\u3001\u914d\u7f6eSSL\u8bc1\u4e66\u3001\u8bbe\u7f6e\u76d1\u542c\u3001\u521b\u5efa\u4ee3\u7406\u670d\u52a1\u3001\u914d\u7f6e\u8ba4\u8bc1\u548c\u65e5\u5fd7\u8bb0\u5f55\uff0c\u4ee5\u53ca\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3002<\/strong> \u5176\u4e2d\uff0c\u914d\u7f6eSSL\u8bc1\u4e66\u5c24\u4e3a\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u6d89\u53ca\u5230\u4ee3\u7406\u670d\u52a1\u5668\u7684\u5b89\u5168\u6027\u53ca\u5176\u80fd\u5426\u6b63\u786e\u5904\u7406HTTPS\u8bf7\u6c42\u3002<\/p>\n<p>\u5728\u8fdb\u884c\u8be6\u7ec6\u914d\u7f6e\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fddNginx\u5df2\u7ecf\u5b89\u88c5\u5728\u4f60\u7684\u670d\u52a1\u5668\u4e0a\uff0c\u5e76\u4e14\u5177\u5907\u5fc5\u8981\u7684\u6a21\u5757\u6765\u8fdb\u884cHTTP\u548cHTTPS\u4ee3\u7406\uff08\u4f8b\u5982\uff0cngx_http_proxy_module\uff09\u3002\u4ee5\u4e0b\u662f\u900f\u660e\u4ee3\u7406\u8bbe\u7f6e\u7684\u6b65\u9aa4\u3002<\/p>\n<p>&#8212;<\/p>\n<p>### \u4e00\u3001\u5b89\u88c5\u548c\u51c6\u5907Nginx<\/p>\n<p>\u5728\u5f00\u59cb\u914d\u7f6e\u4e4b\u524d\uff0c\u5fc5\u987b\u5b89\u88c5Nginx\u3002\u5982\u679c\u4f60\u7684\u670d\u52a1\u5668\u4e0a\u8fd8\u6ca1\u6709Nginx\uff0c\u53ef\u4ee5\u901a\u8fc7\u670d\u52a1\u5668\u7684\u5305\u7ba1\u7406\u7cfb\u7edf\u5b89\u88c5\u5b83\u3002\u5bf9\u4e8e\u5927\u591a\u6570Linux\u53d1\u884c\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528`apt-get`\u6216`yum`\u5b89\u88c5\u3002<\/p>\n<p>&#8220;`shell<\/p>\n<p>sudo apt-get update<\/p>\n<p>sudo apt-get install nginx<\/p>\n<p>&#8220;`<\/p>\n<p>\u4e00\u65e6\u5b89\u88c5\u5b8c\u6210\uff0c\u786e\u4fddNginx\u8fd0\u884c\u8d77\u6765\uff1a<\/p>\n<p>&#8220;`shell<\/p>\n<p>sudo systemctl start nginx<\/p>\n<p>&#8220;`<\/p>\n<p>### \u4e8c\u3001\u914d\u7f6eSSL\u8bc1\u4e66<\/p>\n<p>\u5982\u679c\u9700\u8981\u5904\u7406HTTPS\u6d41\u91cf\uff0c\u4f60\u9700\u8981\u5728Nginx\u4e0a\u914d\u7f6eSSL\u8bc1\u4e66\u3002\u53ef\u4ee5\u4f7f\u7528\u514d\u8d39\u7684Let&#8217;s Encrypt\u8bc1\u4e66\u6216\u8d2d\u4e70\u5546\u4e1a\u8bc1\u4e66\u3002\u786e\u4fdd\u8bc1\u4e66\uff08PEM\u683c\u5f0f\u7684.crt\u6587\u4ef6\uff09\u548c\u79c1\u94a5\uff08.key\u6587\u4ef6\uff09\u653e\u5728\u670d\u52a1\u5668\u7684\u9002\u5f53\u4f4d\u7f6e\uff0c\u5e76\u4fdd\u62a4\u597d\u5b83\u4eec\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>&#8220;`nginx<\/p>\n<p>server {<\/p>\n<p>    listen 443 ssl;<\/p>\n<p>    server_name your_proxy_dom<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.com;<\/p>\n<p>    ssl_certificate \/path\/to\/your\/certificate.crt;<\/p>\n<p>    ssl_certificate_key \/path\/to\/your\/private.key;<\/p>\n<p>    # Rest of your HTTPS configuration<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>### \u4e09\u3001\u8bbe\u7f6eHTTP\u548cHTTPS\u76d1\u542c<\/p>\n<p>\u914d\u7f6eNginx\u4ee5\u76d1\u542cHTTP\uff0880\u7aef\u53e3\uff09\u548cHTTPS\uff08443\u7aef\u53e3\uff09\u6d41\u91cf\u3002<\/p>\n<p>&#8220;`nginx<\/p>\n<p>server {<\/p>\n<p>    listen 80;<\/p>\n<p>    server_name your_proxy_domain.com;<\/p>\n<p>    location \/ {<\/p>\n<p>        proxy_pass http:\/\/upstream_server;<\/p>\n<p>        # Rest of your HTTP configuration<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>server {<\/p>\n<p>    listen 443 ssl;<\/p>\n<p>    server_name your_proxy_domain.com;<\/p>\n<p>    ssl_certificate \/path\/to\/your\/certificate.crt;<\/p>\n<p>    ssl_certificate_key \/path\/to\/your\/private.key;<\/p>\n<p>    location \/ {<\/p>\n<p>        proxy_pass https:\/\/upstream_server;<\/p>\n<p>        # Rest of your HTTPS configuration<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>### \u56db\u3001\u521b\u5efa\u4ee3\u7406\u670d\u52a1\u914d\u7f6e<\/p>\n<p>\u5728Nginx\u4e2d\u521b\u5efa\u6b63\u786e\u7684\u4ee3\u7406\u8f6c\u53d1\u89c4\u5219\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u901a\u8fc7\u4f7f\u7528`proxy_pass`\u6307\u4ee4\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u540e\u7aef\u670d\u52a1\u7528\u6765\u8f6c\u53d1\u8bf7\u6c42\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>&#8220;`nginx<\/p>\n<p>location \/ {<\/p>\n<p>    proxy_set_header Host $host;<\/p>\n<p>    proxy_set_header X-Real-IP $remote_addr;<\/p>\n<p>    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<\/p>\n<p>    proxy_set_header X-Forwarded-Proto $scheme;<\/p>\n<p>    proxy_pass http:\/\/upstream_server;<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p><strong>\u4fdd\u8bc1\u5e94\u7528\u6b63\u786e\u7684\u5934\u6587\u4ef6\u4f20\u9012\uff0c\u4ee5\u786e\u4fdd\u540e\u7aef\u670d\u52a1\u5668\u63a5\u6536\u5230\u6b63\u786e\u7684\u6e90\u4fe1\u606f\u662f\u5f88\u91cd\u8981\u7684\u3002<\/strong><\/p>\n<p>### \u4e94\u3001\u914d\u7f6e\u8ba4\u8bc1\u548c\u5b89\u5168\u6027<\/p>\n<p>\u4e3a\u4e86\u786e\u4fdd\u53ea\u6709\u6388\u6743\u7528\u6237\u80fd\u901a\u8fc7\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u5728Nginx\u914d\u7f6e\u57fa\u672c\u8ba4\u8bc1\u6216\u5176\u4ed6\u66f4\u9ad8\u7ea7\u7684\u8ba4\u8bc1\u673a\u5236\u3002<\/p>\n<p>&#8220;`nginx<\/p>\n<p>location \/ {<\/p>\n<p>    auth_basic &#8220;Restricted Content&#8221;;<\/p>\n<p>    auth_basic_user_file \/etc\/nginx\/.htpasswd;<\/p>\n<p>    proxy_pass http:\/\/upstream_server;<\/p>\n<p>    # Rest of your proxy configuration<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u6b64\u5916\uff0c\u914d\u7f6e\u5b89\u5168\u6027\u76f8\u5173\u7684\u5934\u548c\u9650\u5236\u5bf9\u63d0\u5347\u4ee3\u7406\u670d\u52a1\u5668\u5b89\u5168\u6027\u4e5f\u5f88\u91cd\u8981\uff0c\u6bd4\u5982\u4f7f\u7528`add_header`\u6765\u6dfb\u52a0HSTS\u5934\uff0c\u4ee5\u53ca\u4f7f\u7528\u9650\u5236\u901f\u7387\u548c\u8fde\u63a5\u6570\u7b49\u6765\u62b5\u5fa1DDoS\u653b\u51fb\u3002<\/p>\n<p>### \u516d\u3001\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/p>\n<p>\u65e5\u5fd7\u8bb0\u5f55\u5bf9\u4e8e\u8ddf\u8e2a\u548c\u5206\u6790\u901a\u8fc7\u4ee3\u7406\u670d\u52a1\u5668\u7684\u6d41\u91cf\u81f3\u5173\u91cd\u8981\u3002\u5728Nginx\u914d\u7f6e\u4e2d\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u65e5\u5fd7\u683c\u5f0f\u6765\u8bb0\u5f55\u6240\u9700\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p>&#8220;`nginx<\/p>\n<p>http {<\/p>\n<p>    log_format proxy &#8216;[$time_local] $remote_addr &#8211; $remote_user &#8211; $status &#8216;<\/p>\n<p>                     &#8216;$body_bytes_sent &#8220;$request&#8221; &#8220;$http_referer&#8221; &#8216;<\/p>\n<p>                     &#8216;&#8221;$http_user_agent&#8221; &#8220;$http_x_forwarded_for&#8221;&#8216;;<\/p>\n<p>    access_log \/var\/log\/nginx\/access.log proxy;<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>### \u4e03\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/p>\n<p>\u5728\u6240\u6709\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u4e0d\u8981\u5fd8\u8bb0\u6d4b\u8bd5Nginx\u914d\u7f6e\u6587\u4ef6\u4ee5\u786e\u4fdd\u6ca1\u6709\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<p>&#8220;`shell<\/p>\n<p>sudo nginx -t<\/p>\n<p>&#8220;`<\/p>\n<p>\u5982\u679c\u6d4b\u8bd5\u901a\u8fc7\uff0c\u5219\u53ef\u4ee5\u91cd\u65b0\u52a0\u8f7dNginx\u4ee5\u5e94\u7528\u65b0\u7684\u914d\u7f6e\u3002<\/p>\n<p>&#8220;`shell<\/p>\n<p>sudo systemctl reload nginx<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728Nginx\u90e8\u7f72\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982`curl`\u6216\u4ee3\u7406\u6d4b\u8bd5\u7f51\u7ad9\u6765\u68c0\u67e5\u4ee3\u7406\u670d\u52a1\u662f\u5426\u80fd\u591f\u6b63\u786e\u7684\u5904\u7406HTTP\u548cHTTPS\u8bf7\u6c42\u3002<\/p>\n<p>&#8212;<\/p>\n<p><strong>\u914d\u7f6eNginx\u4f5c\u4e3a\u900f\u660e\u4ee3\u7406\u65f6\uff0c\u8ba4\u8bc1\u548c\u5b89\u5168\u6027\u8bbe\u7f6e\u662f\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u4eec\u4fdd\u62a4\u4e86\u4ee3\u7406\u670d\u52a1\u4e0d\u88ab\u672a\u7ecf\u6388\u6743\u7684\u8bbf\u95ee\uff0c\u5e76\u786e\u4fdd\u901a\u8fc7\u4ee3\u7406\u4f20\u8f93\u7684\u6570\u636e\u5b89\u5168\u3002<\/strong> \u800c\u901a\u8fc7\u7ed3\u5408\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7Nginx\u6210\u529f\u5730\u521b\u5efa\u4e00\u4e2a\u900f\u660e\u4ee3\u7406\uff0c\u65e2\u80fd\u5904\u7406HTTP\u4e5f\u80fd\u5904\u7406HTTPS\u8bf7\u6c42\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u76d1\u63a7\u5e76\u5b9a\u671f\u7ef4\u62a4\u4f60\u7684Nginx\u4ee3\u7406\u670d\u52a1\u5668\u662f\u63d0\u4f9b\u7a33\u5b9a\u548c\u5b89\u5168\u670d\u52a1\u7684\u91cd\u8981\u73af\u8282\u3002<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728 Nginx \u4e2d\u8bbe\u7f6e\u900f\u660e\u7684 HTTP \u4ee3\u7406\uff1f<\/strong><\/p>\n<p>\u8981\u5728 Nginx \u4e2d\u914d\u7f6e\u4e00\u4e2a\u900f\u660e\u7684 HTTP \u4ee3\u7406\uff0c\u60a8\u9700\u8981\u7f16\u8f91 Nginx \u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u901a\u5e38\u4e3a `nginx.conf` \u6587\u4ef6\u3002\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 `proxy_pass` \u6307\u4ee4\u5c06\u8bf7\u6c42\u4ee3\u7406\u5230\u53e6\u4e00\u4e2a HTTP \u670d\u52a1\u5668\u3002\u786e\u4fdd\u5728\u4ee3\u7406\u6a21\u5757\u4e2d\u542f\u7528\u900f\u660e\u4ee3\u7406\u7684\u76f8\u5173\u8bbe\u7f6e\uff0c\u8bbe\u7f6e\u900f\u660e\u4ee3\u7406\u53ef\u4ee5\u8ba9\u5ba2\u6237\u7aef\u65e0\u611f\u77e5\u5730\u4e0e\u4ee3\u7406\u670d\u52a1\u5668\u901a\u4fe1\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728 Nginx \u4e2d\u8bbe\u7f6e\u900f\u660e\u7684 HTTPS \u4ee3\u7406\uff1f<\/strong><\/p>\n<p>\u8981\u5728 Nginx \u4e2d\u914d\u7f6e\u900f\u660e\u7684 HTTPS \u4ee3\u7406\uff0c\u60a8\u9700\u8981\u914d\u7f6e SSL \u8bc1\u4e66\u548c\u5bc6\u94a5\uff0c\u5e76\u5c06\u5176\u4e0e\u4ee3\u7406\u670d\u52a1\u5668\u76f8\u5173\u8054\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 `proxy_pass` \u6307\u4ee4\u5c06\u4f20\u5165\u7684 HTTPS \u8bf7\u6c42\u4ee3\u7406\u5230\u53e6\u4e00\u4e2a HTTPS \u670d\u52a1\u5668\u3002\u786e\u4fdd\u5728\u914d\u7f6e HTTPS \u4ee3\u7406\u65f6\uff0c\u60a8\u7684 SSL \u914d\u7f6e\u662f\u6b63\u786e\u7684\uff0c\u5e76\u4e14\u4ee3\u7406\u670d\u52a1\u5668\u80fd\u591f\u6b63\u5e38\u5904\u7406\u52a0\u5bc6\u6d41\u91cf\u3002<\/p>\n<p><strong>Nginx \u900f\u660e\u4ee3\u7406\u7684\u4f18\u7f3a\u70b9\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u900f\u660e\u4ee3\u7406\u5141\u8bb8\u5ba2\u6237\u7aef\u65e0\u611f\u77e5\u5730\u4e0e\u4ee3\u7406\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\uff0c\u5b83\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u548c\u5b89\u5168\u7b56\u7565\uff0c\u540c\u65f6\u9690\u85cf\u771f\u5b9e\u670d\u52a1\u5668\u7684 IP \u5730\u5740\u3002\u7136\u800c\uff0c\u900f\u660e\u4ee3\u7406\u4e5f\u53ef\u80fd\u5bfc\u81f4\u4e00\u4e9b\u95ee\u9898\uff0c\u5982\u589e\u52a0\u7f51\u7edc\u7684\u590d\u6742\u6027\u3001\u5f71\u54cd\u7f51\u7edc\u6027\u80fd\u7b49\u3002\u5728\u8bbe\u7f6e\u900f\u660e\u4ee3\u7406\u65f6\uff0c\u9700\u8981\u5168\u9762\u8bc4\u4f30\u5176\u4f18\u7f3a\u70b9\uff0c\u786e\u4fdd\u5176\u7b26\u5408\u7f51\u7edc\u9700\u6c42\u548c\u5b89\u5168\u6807\u51c6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u914d\u7f6e Nginx \u4ee5\u7528\u4f5c\u900f\u660eHTTP\u548cHTTPS\u4ee3\u7406\u65f6\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u786e\u5b9a\u4ee3\u7406\u9700\u6c42\u3001\u5b89\u88c5Nginx\u3001\u914d\u7f6eSSL [&hellip;]","protected":false},"author":1,"featured_media":77118,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/77117"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=77117"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/77117\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/77118"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=77117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=77117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=77117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}