{"id":442588,"date":"2026-04-21T16:10:42","date_gmt":"2026-04-21T08:10:42","guid":{"rendered":"https:\/\/idc.net\/help\/?p=442588"},"modified":"2026-04-18T16:13:22","modified_gmt":"2026-04-18T08:13:22","slug":"%e9%a6%99%e6%b8%afvps%e6%90%ad%e5%bb%banginx%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%ef%bc%9a%e5%a4%9a%e5%9f%9f%e5%90%8d%e9%85%8d%e7%bd%ae%e4%b8%8essl%e8%af%81%e4%b9%a6%e7%94%b3%e8%af%b7%e5%ae%8c%e6%95%b4","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/442588\/","title":{"rendered":"\u9999\u6e2fVPS\u642d\u5efaNginx\u53cd\u5411\u4ee3\u7406\uff1a\u591a\u57df\u540d\u914d\u7f6e\u4e0eSSL\u8bc1\u4e66\u7533\u8bf7\u5b8c\u6574\u6559\u7a0b"},"content":{"rendered":"<p>\u5f88\u591a\u4eba\u5728\u9999\u6e2f VPS \u4e0a\u8dd1\u4e86\u591a\u4e2a\u9879\u76ee\uff0c\u4f46\u6bcf\u4e2a\u9879\u76ee\u7528\u4e0d\u540c\u7aef\u53e3\uff08\u5982 :3000\u3001:8080\uff09\u8bbf\u95ee\uff0c\u65e2\u4e0d\u7f8e\u89c2\u53c8\u96be\u8bb0\u3002\u901a\u8fc7 <strong>Nginx \u53cd\u5411\u4ee3\u7406<\/strong>\uff0c\u4f60\u53ef\u4ee5\u8ba9\u591a\u4e2a\u57df\u540d\u5171\u4eab\u540c\u4e00\u53f0 VPS \u7684 80\/443 \u7aef\u53e3\uff0c\u540c\u65f6\u4e3a\u6bcf\u4e2a\u57df\u540d\u914d\u7f6e\u72ec\u7acb\u7684\u514d\u8d39 SSL \u8bc1\u4e66\uff0c\u5b9e\u73b0\u4e13\u4e1a\u7ea7\u7684 HTTPS \u8bbf\u95ee\u3002\u672c\u6559\u7a0b\u9002\u5408\u6709\u57fa\u672c Linux \u547d\u4ee4\u57fa\u7840\u7684\u8bfb\u8005\u3002<\/p>\n<hr \/>\n<h2>\u4e00\u3001\u73af\u5883\u51c6\u5907<\/h2>\n<ul>\n<li>\u9999\u6e2f VPS \u4e00\u53f0\uff08Ubuntu 22.04 LTS \u63a8\u8350\uff09<\/li>\n<li>\u5df2\u89e3\u6790\u5230\u8be5 VPS IP \u7684\u57df\u540d\uff08\u5982 <code>site1.com<\/code>\u3001<code>site2.com<\/code>\uff09<\/li>\n<li>\u5177\u5907 sudo \u6743\u9650\u7684 SSH \u7528\u6237<\/li>\n<\/ul>\n<p>\u786e\u8ba4 VPS \u4e0a\u6ca1\u6709\u5176\u4ed6\u7a0b\u5e8f\u5360\u7528 80\/443 \u7aef\u53e3\uff1a<\/p>\n<pre><code>sudo ss -tlnp | grep -E ':80|:443'<\/code><\/pre>\n<p>\u5982\u679c\u6709\u8f93\u51fa\uff0c\u9700\u8981\u5148\u505c\u6b62\u5360\u7528\u7aef\u53e3\u7684\u670d\u52a1\u3002<\/p>\n<hr \/>\n<h2>\u4e8c\u3001\u5b89\u88c5 Nginx<\/h2>\n<pre><code>sudo apt update\r\nsudo apt install nginx -y\r\nsudo systemctl enable nginx\r\nsudo systemctl start nginx<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8bbf\u95ee\u4f60\u7684 VPS IP\uff0c\u80fd\u770b\u5230 Nginx \u6b22\u8fce\u9875\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<hr \/>\n<h2>\u4e09\u3001\u7406\u89e3 Nginx \u53cd\u5411\u4ee3\u7406\u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>\u53cd\u5411\u4ee3\u7406\u7684\u6838\u5fc3\u903b\u8f91\u662f\uff1a<\/p>\n<ol>\n<li>\u7528\u6237\u8bbf\u95ee <code>https:\/\/site1.com<\/code><\/li>\n<li>Nginx \u76d1\u542c 443 \u7aef\u53e3\uff0c\u6839\u636e\u57df\u540d\u5224\u65ad\u8bf7\u6c42\u5c5e\u4e8e\u54ea\u4e2a\u9879\u76ee<\/li>\n<li>Nginx \u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230\u672c\u5730\u5bf9\u5e94\u7684\u7aef\u53e3\uff08\u5982 <code>127.0.0.1:3000<\/code>\uff09<\/li>\n<li>\u672c\u5730\u5e94\u7528\u5904\u7406\u8bf7\u6c42\u540e\uff0cNginx \u5c06\u54cd\u5e94\u8fd4\u56de\u7ed9\u7528\u6237<\/li>\n<\/ol>\n<p>\u6574\u4e2a\u8fc7\u7a0b\u5bf9\u7528\u6237\u900f\u660e\uff0c\u7528\u6237\u53ea\u770b\u5230\u57df\u540d\uff0c\u770b\u4e0d\u5230\u5185\u90e8\u7aef\u53e3\u3002<\/p>\n<hr \/>\n<h2>\u56db\u3001\u521b\u5efa\u7b2c\u4e00\u4e2a\u7ad9\u70b9\u7684 Nginx \u914d\u7f6e\u6587\u4ef6<\/h2>\n<p>Nginx \u7684\u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6\u5b58\u653e\u5728 <code>\/etc\/nginx\/sites-available\/<\/code> \u76ee\u5f55\u3002\u4e3a\u6bcf\u4e2a\u57df\u540d\u521b\u5efa\u72ec\u7acb\u7684\u914d\u7f6e\u6587\u4ef6\u662f\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<pre><code>sudo nano \/etc\/nginx\/sites-available\/site1.com<\/code><\/pre>\n<p>\u5199\u5165\u4ee5\u4e0b\u5185\u5bb9\uff08\u5148\u914d\u7f6e HTTP\uff0c\u540e\u7eed\u518d\u5347\u7ea7 HTTPS\uff09\uff1a<\/p>\n<pre><code>server {\r\n    listen 80;\r\n    server_name site1.com www.site1.com;\r\n\r\n    location \/ {\r\n        proxy_pass http:\/\/127.0.0.1:3000;\r\n        proxy_http_version 1.1;\r\n        proxy_set_header Upgrade $http_upgrade;\r\n        proxy_set_header Connection 'upgrade';\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        proxy_set_header X-Forwarded-Proto $scheme;\r\n        proxy_cache_bypass $http_upgrade;\r\n    }\r\n}<\/code><\/pre>\n<p>\u542f\u7528\u8be5\u7ad9\u70b9\u914d\u7f6e\uff08\u521b\u5efa\u8f6f\u94fe\u63a5\uff09\uff1a<\/p>\n<pre><code>sudo ln -s \/etc\/nginx\/sites-available\/site1.com \/etc\/nginx\/sites-enabled\/\r\nsudo nginx -t        # \u68c0\u67e5\u914d\u7f6e\u8bed\u6cd5\r\nsudo systemctl reload nginx<\/code><\/pre>\n<hr \/>\n<h2>\u4e94\u3001\u914d\u7f6e\u7b2c\u4e8c\u4e2a\u57df\u540d\uff08\u591a\u57df\u540d\u6838\u5fc3\uff09<\/h2>\n<p>\u591a\u57df\u540d\u914d\u7f6e\u4e0e\u7b2c\u4e00\u4e2a\u5b8c\u5168\u76f8\u540c\uff0c\u53ea\u9700\u4fee\u6539 <code>server_name<\/code> \u548c <code>proxy_pass<\/code> \u7aef\u53e3\uff1a<\/p>\n<pre><code>sudo nano \/etc\/nginx\/sites-available\/site2.com<\/code><\/pre>\n<pre><code>server {\r\n    listen 80;\r\n    server_name site2.com www.site2.com;\r\n\r\n    location \/ {\r\n        proxy_pass http:\/\/127.0.0.1:4000;   # \u6307\u5411\u7b2c\u4e8c\u4e2a\u5e94\u7528\u7684\u7aef\u53e3\r\n        proxy_http_version 1.1;\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        proxy_set_header X-Forwarded-Proto $scheme;\r\n    }\r\n}<\/code><\/pre>\n<pre><code>sudo ln -s \/etc\/nginx\/sites-available\/site2.com \/etc\/nginx\/sites-enabled\/\r\nsudo nginx -t\r\nsudo systemctl reload nginx<\/code><\/pre>\n<p>\u6309\u6b64\u65b9\u5f0f\uff0c\u4e00\u53f0\u9999\u6e2f VPS \u53ef\u4ee5\u540c\u65f6\u6258\u7ba1\u6570\u5341\u4e2a\u4e0d\u540c\u57df\u540d\u7684\u7f51\u7ad9\uff0c\u53ea\u9700\u4fdd\u8bc1\u5404\u5e94\u7528\u4f7f\u7528\u4e0d\u540c\u672c\u5730\u7aef\u53e3\u5373\u53ef\u3002<\/p>\n<hr \/>\n<h2>\u516d\u3001\u7533\u8bf7 Let's Encrypt \u514d\u8d39 SSL \u8bc1\u4e66<\/h2>\n<p>\u4f7f\u7528 Certbot \u5de5\u5177\u53ef\u4ee5\u4e00\u952e\u7533\u8bf7\u5e76\u914d\u7f6e Let's Encrypt \u8bc1\u4e66\uff0c\u5b8c\u5168\u514d\u8d39\uff0c\u6709\u6548\u671f 90 \u5929\uff0c\u652f\u6301\u81ea\u52a8\u7eed\u671f\u3002<\/p>\n<h3>Step 1\uff1a\u5b89\u88c5 Certbot<\/h3>\n<pre><code>sudo apt install certbot python3-certbot-nginx -y<\/code><\/pre>\n<h3>Step 2\uff1a\u4e3a\u57df\u540d\u7533\u8bf7\u8bc1\u4e66\u5e76\u81ea\u52a8\u914d\u7f6e Nginx<\/h3>\n<pre><code>sudo certbot --nginx -d site1.com -d www.site1.com<\/code><\/pre>\n<p>Certbot \u4f1a\u81ea\u52a8\uff1a<\/p>\n<ul>\n<li>\u5411 Let's Encrypt \u9a8c\u8bc1\u57df\u540d\u6240\u6709\u6743\uff08\u9700\u8981\u57df\u540d\u5df2\u89e3\u6790\u5230\u8be5 VPS\uff09<\/li>\n<li>\u4e0b\u8f7d SSL \u8bc1\u4e66\u6587\u4ef6<\/li>\n<li>\u4fee\u6539 Nginx \u914d\u7f6e\uff0c\u6dfb\u52a0 443 \u7aef\u53e3\u76d1\u542c\u548c\u8bc1\u4e66\u8def\u5f84<\/li>\n<li>\u5c06 HTTP \u6d41\u91cf\u81ea\u52a8\u91cd\u5b9a\u5411\u5230 HTTPS<\/li>\n<\/ul>\n<h3>Step 3\uff1a\u4e3a\u7b2c\u4e8c\u4e2a\u57df\u540d\u7533\u8bf7\u8bc1\u4e66<\/h3>\n<pre><code>sudo certbot --nginx -d site2.com -d www.site2.com<\/code><\/pre>\n<h3>Step 4\uff1a\u9a8c\u8bc1\u81ea\u52a8\u7eed\u671f\u662f\u5426\u914d\u7f6e\u6210\u529f<\/h3>\n<pre><code>sudo certbot renew --dry-run<\/code><\/pre>\n<p>\u770b\u5230 <code>Congratulations, all simulated renewals succeeded<\/code> \u8bf4\u660e\u81ea\u52a8\u7eed\u671f\u5df2\u914d\u7f6e\u597d\uff0c\u65e0\u9700\u624b\u52a8\u5e72\u9884\u3002<\/p>\n<hr \/>\n<h2>\u4e03\u3001\u914d\u7f6e\u540e\u7684 HTTPS \u7248 Nginx \u914d\u7f6e\u793a\u4f8b<\/h2>\n<p>Certbot \u81ea\u52a8\u4fee\u6539\u540e\uff0c\u4f60\u7684 <code>site1.com<\/code> \u914d\u7f6e\u6587\u4ef6\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n<pre><code>server {\r\n    listen 80;\r\n    server_name site1.com www.site1.com;\r\n    return 301 https:\/\/$host$request_uri;   # HTTP \u5f3a\u5236\u8df3\u8f6c HTTPS\r\n}\r\n\r\nserver {\r\n    listen 443 ssl;\r\n    server_name site1.com www.site1.com;\r\n\r\n    ssl_certificate \/etc\/letsencrypt\/live\/site1.com\/fullchain.pem;\r\n    ssl_certificate_key \/etc\/letsencrypt\/live\/site1.com\/privkey.pem;\r\n    include \/etc\/letsencrypt\/options-ssl-nginx.conf;\r\n\r\n    location \/ {\r\n        proxy_pass http:\/\/127.0.0.1:3000;\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        proxy_set_header X-Forwarded-Proto $scheme;\r\n    }\r\n}<\/code><\/pre>\n<hr \/>\n<h2>\u516b\u3001\u5e38\u89c1\u95ee\u9898\u6392\u67e5<\/h2>\n<h3>\u95ee\u9898\u4e00\uff1a\u7533\u8bf7\u8bc1\u4e66\u65f6\u63d0\u793a\u57df\u540d\u9a8c\u8bc1\u5931\u8d25<\/h3>\n<p>\u539f\u56e0\uff1a\u57df\u540d DNS \u672a\u89e3\u6790\u5230 VPS IP\uff0c\u6216\u89e3\u6790\u5c1a\u672a\u751f\u6548\uff08\u901a\u5e38\u9700\u8981 5~30 \u5206\u949f\uff09\u3002\u68c0\u67e5\u65b9\u6cd5\uff1a<\/p>\n<pre><code>ping site1.com   # \u770b\u8fd4\u56de\u7684 IP \u662f\u5426\u662f\u4f60\u7684 VPS IP<\/code><\/pre>\n<h3>\u95ee\u9898\u4e8c\uff1aNginx \u91cd\u8f7d\u540e\u63d0\u793a 80 \u7aef\u53e3\u88ab\u5360\u7528<\/h3>\n<p>\u5e38\u89c1\u539f\u56e0\u662f\u7cfb\u7edf\u9884\u88c5\u4e86 Apache\uff0c\u4e0e Nginx \u51b2\u7a81\uff1a<\/p>\n<pre><code>sudo systemctl stop apache2\r\nsudo systemctl disable apache2<\/code><\/pre>\n<h3>\u95ee\u9898\u4e09\uff1a\u53cd\u5411\u4ee3\u7406\u540e\u5e94\u7528\u83b7\u53d6\u4e0d\u5230\u771f\u5b9e IP<\/h3>\n<p>\u786e\u4fdd Nginx \u914d\u7f6e\u4e2d\u5305\u542b\u4ee5\u4e0b header \u4f20\u9012\uff1a<\/p>\n<pre><code>proxy_set_header X-Real-IP $remote_addr;\r\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<\/code><\/pre>\n<p>\u5e94\u7528\u4ee3\u7801\u4e2d\u8bfb\u53d6 <code>X-Forwarded-For<\/code> \u5934\u800c\u975e\u76f4\u63a5\u8bfb\u53d6 <code>REMOTE_ADDR<\/code>\u3002<\/p>\n<hr \/>\n<h2>\u4e5d\u3001\u6027\u80fd\u4f18\u5316\u5efa\u8bae<\/h2>\n<ul>\n<li><strong>\u5f00\u542f Gzip \u538b\u7f29<\/strong>\uff1a\u5728 <code>\/etc\/nginx\/nginx.conf<\/code> \u4e2d\u542f\u7528 <code>gzip on;<\/code>\uff0c\u53ef\u51cf\u5c11 60~80% \u7684\u6587\u672c\u5185\u5bb9\u4f20\u8f93\u91cf<\/li>\n<li><strong>\u914d\u7f6e\u6d4f\u89c8\u5668\u7f13\u5b58<\/strong>\uff1a\u5bf9\u9759\u6001\u6587\u4ef6\uff08\u56fe\u7247\u3001CSS\u3001JS\uff09\u8bbe\u7f6e <code>expires 30d;<\/code>\uff0c\u51cf\u5c11\u91cd\u590d\u8bf7\u6c42<\/li>\n<li><strong>\u542f\u7528 HTTP\/2<\/strong>\uff1a\u5728 <code>listen 443 ssl;<\/code> \u6539\u4e3a <code>listen 443 ssl http2;<\/code>\uff0c\u63d0\u5347\u5e76\u53d1\u52a0\u8f7d\u6548\u7387<\/li>\n<li><strong>\u9650\u5236\u8bf7\u6c42\u901f\u7387<\/strong>\uff1a\u4f7f\u7528 <code>limit_req_zone<\/code> \u9632\u6b62\u722c\u866b\u548c CC \u653b\u51fb\u5360\u7528\u8d44\u6e90<\/li>\n<\/ul>\n<hr \/>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7 Nginx \u53cd\u5411\u4ee3\u7406 + Let's Encrypt SSL\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u53f0<a title=\"\u9999\u6e2f\u4e91\u670d\u52a1\u5668\" href=\"https:\/\/idc.net\/cloud-hk\/\">\u9999\u6e2f VPS<\/a> \u4e0a\u4ee5\u6700\u4f4e\u6210\u672c\u6258\u7ba1\u591a\u4e2a HTTPS \u7f51\u7ad9\uff0c\u6bcf\u4e2a\u57df\u540d\u72ec\u7acb\u914d\u7f6e\uff0c\u4e92\u4e0d\u5e72\u6270\u3002\u6574\u4e2a\u914d\u7f6e\u6d41\u7a0b\u901a\u5e38\u5728 30~60 \u5206\u949f\u5185\u5b8c\u6210\uff0c\u662f\u51fa\u6d77\u5efa\u7ad9\u8fd0\u7ef4\u7684\u5fc5\u5907\u6280\u80fd\u3002<\/p>\n<p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u9999\u6e2f VPS\uff0c\u540e\u6d6a\u4e91\u63d0\u4f9b CN2 GIA \u4f18\u8d28\u7ebf\u8def\uff0c\u6700\u4f4e 10 \u5143\/\u6708\uff0c\u652f\u6301\u652f\u4ed8\u5b9d\u4ed8\u6b3e\uff0c<a title=\"\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u5957\u9910\" href=\"https:\/\/idc.net\/cloud-hk\/\">\u67e5\u770b\u5957\u9910\u8be6\u60c5<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u4eba\u5728\u9999\u6e2f VPS \u4e0a\u8dd1\u4e86\u591a\u4e2a\u9879\u76ee\uff0c\u4f46\u6bcf\u4e2a\u9879\u76ee\u7528\u4e0d\u540c\u7aef\u53e3\uff08\u5982 :3000\u3001:8080\uff09\u8bbf\u95ee\uff0c\u65e2\u4e0d\u7f8e\u89c2\u53c8\u96be\u8bb0\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[202648],"tags":[],"class_list":["post-442588","post","type-post","status-publish","format-standard","hentry","category-hkvps"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/442588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=442588"}],"version-history":[{"count":1,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/442588\/revisions"}],"predecessor-version":[{"id":442589,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/442588\/revisions\/442589"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=442588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=442588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=442588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}