您只需要在 NGINX 中使用 SSL 证书。此外,NGINX 可用作 Node.js 服务器的负载均衡器,而且非常易于配置。
将所有 http 流量重定向到 https 也非常简单。
从我的服务器检查以下配置文件。我加了cmets,希望通俗易懂:
http {
# ......
# add proxy, gzip and other http settings here //
# ......
# running node.js servers for nginx proxy - servers are choosen randomly and can be used as load balancers
# You can add as many servers as you want or just use one, but all servers must be running same script
# if you want to add servers with different script, just add new upstream and link it to other location in server settings
upstream example_com {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
keepalive 64;
}
# listen http on port 80 and redirect all requested urls to https server
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
# listen https on port 443 and proxy Node.js servers
server {
listen 443 ssl;
# SSL Certificate settings
ssl_certificate /ssl_cert/location/example.com.bundle.crt;
ssl_certificate_key /ssl_cert/location/example.com.key;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
server_name example.com www.example.com;
# ......
# add location, rewrite, error handling, public folders for static files served from directly NGINX and other settings here
# ......
# redirect traffic to Node.js servers.
location / {
proxy_redirect off;
# Proxy original headers to Node.js
# for example if you want to get client IP address from Node.js, there is no way without redirecting headers from NGINX
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
# Add comment (#) to "proxy_cache one;" and "proxy_cache_key sfs$request_uri$scheme;" if you want to serve files more than 2MB from Node.js
proxy_cache one;
proxy_cache_key sfs$request_uri$scheme;
# upstream name with Node.js servers in it
# no need to install SSL for Node.js, you can use http:// between NGINX and Node.js, all traffic to client will be encrypted by NGINX anyway
proxy_pass http://example_com;
}
}
}
要在同一域下使用多个应用,请使用上面的配置文件,只需为其他应用添加 upstream 和 location
http {
# ..........
upstream example_com {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
keepalive 64;
}
upstream example_com_second_app {
server 127.0.0.1:3002;
server 127.0.0.1:3003;
keepalive 64;
}
# ..........
server {
# ..........
location / {
# ..........
proxy_pass http://example_com;
}
location /second_app/ {
# ..........
proxy_pass http://example_com_second_app;
}
}
}
要在同一台服务器上使用不同的域和 SSL 证书,您可以使用相同的配置并添加具有不同 server_name
的另一台服务器
http {
# ..........
# Configure server for example.com
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
# SSL Certificate settings
ssl_certificate /ssl_cert/location/example.com.bundle.crt;
ssl_certificate_key /ssl_cert/location/example.com.key;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
server_name example.com www.example.com;
# ..........
}
# Configure server for my_example.com
server {
listen 80;
server_name my_example.com www.my_example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
# SSL Certificate settings
ssl_certificate /ssl_cert/location/my_example.com.bundle.crt;
ssl_certificate_key /ssl_cert/location/my_example.com.key;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
server_name my_example.com www.my_example.com;
# ..........
}
}
您甚至可以为不同的域使用相同的上游。
NGINX 真的很酷、免费、最快且易于配置:)