文章目录
简介:
这是我自己的万金油模板,任意一个站点,照下面来设置就不会有错,然后再根据网站的实际需要补充其它配置就行了。
配置:
upstream wordpress { # 根据反向代理的站点随意命名
server 127.0.0.1:9092; # 你要反向代理站点的 HTTP 协议端口号
}
server {
listen 80; # 监听 ipv4 HTTP 协议默认的 80 端口
listen [::]:80; # 监听 ipv6 HTTP 协议默认的 80 端口
listen 443 ssl http2; # 监听 ipv4 HTTPS 协议默认的 443 端口、 添加 http2 支持
listen [::]:443 ssl http2; # 监听 ipv6 HTTPS 协议默认的 443 端口、 添加 http2 支持
# 运营商默认会封家庭宽带的 80 和 443 端口,家里用记得改成别的端口。
# 如果你是商业宽带,或者服务器托管在机房,就可以用这两个端口。
server_name localhost; # 拥有自有域名的可将 localhost 修改为域名,例如 xxxx.xxxx.com
http2_idle_timeout 5m; # up from 3m default
client_max_body_size 20m; # 调整 nginx 文件上传大小限制,默认 1m 肯定不合适。
if ($scheme != "https") { # 强制使用 https 协议,通过 http 访问自动跳转去 https
return 301 https://$host$request_uri;
}
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; # websocket 的通用配置(某些网站可能需要特殊配置)
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
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_pass http://wordpress; # 根据反向代理的站点随意命名,同上
}
ssl_certificate /etc/letsencrypt/default/fullchain.pem; # 拥有自有域名的请配置为自己的证书路径
ssl_certificate_key /etc/letsencrypt/default/privkey.pem; # 拥有自有域名的请配置为自己的证书路径
include /etc/letsencrypt/options-ssl-nginx.conf; # letsencrypt nginx 的安全配置路径
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # letsencrypt dhparams 证书路径
# Add HSTS header with preload. This is the line that does it.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"; # 申明站点只接受 HTTPS 访问
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
备注:
没条件搞 SSL 证书的,这些配置可以去掉
ssl_certificate /etc/letsencrypt/default/fullchain.pem; # 拥有自有域名的请配置为自己的证书路径
ssl_certificate_key /etc/letsencrypt/default/privkey.pem; # 拥有自有域名的请配置为自己的证书路径
include /etc/letsencrypt/options-ssl-nginx.conf; # letsencrypt nginx 的安全配置路径
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # letsencrypt dhparams 证书路径
# Add HSTS header with preload. This is the line that does it.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"; # 申明站点只接受 HTTPS 访问