♾️ text 代码:server {
listen 80;
server_name emolo.cn www.emolo.cn;
# 301 重定向到 HTTPS
return 301 https://emolo.cn$request_uri;
}
server {
listen 443 ssl;
server_name emolo.cn;
# SSL 配置
ssl_certificate /cart/emolo.cn_nginx/emolo.cn_bundle.crt;
ssl_certificate_key /cart/emolo.cn_nginx/emolo.cn.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2; # 只使用 TLS 1.2
ssl_ciphers 'HIGH:!DH:!EXPORT:!RC4:!LOW:!aNULL:!eNULL'; # 更加安全的加密套件
ssl_prefer_server_ciphers on;
# Gzip 设置
gzip on;
gzip_min_length 100;
gzip_comp_level 6;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml image/jpeg image/gif image/png;
gzip_static on;
gzip_vary on;
gzip_buffers 32 4k;
gzip_http_version 1.1;
# URL 重写,确保非 www 请求重定向到非 www
if ($host != 'emolo.cn') {
return 301 https://emolo.cn$request_uri;
}
location / {
root /usr/share/nginx/dist;
add_header Cache-Control no-store always;
try_files $uri /index.html;
}
location ~ ^/(api|gravatar|feed\.xml) {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /websocket {
proxy_pass http://localhost:8080/websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header 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_set_header Origin "";
proxy_read_timeout 86400s;
proxy_send_timeout 60s;
proxy_connect_timeout 60s;
}
# 加载默认配置
include /etc/nginx/default.d/*.conf;
}
以下是DeepSeek给我做出的修改,大家看着学习吧,若有问题请指出
♾️ text 代码:# HTTP强制跳转HTTPS(同时处理www和非www)
server {
listen 80;
server_name emolo.cn www.emolo.cn;
return 301 https://emolo.cn$request_uri; # 统一跳转到无www的HTTPS
}
# HTTPS www跳转非www(需独立server块)
server {
listen 443 ssl;
server_name www.emolo.cn; # 专门处理www的HTTPS请求
ssl_certificate /cart/emolo.cn_nginx/emolo.cn_bundle.crt;
ssl_certificate_key /cart/emolo.cn_nginx/emolo.cn.key;
return 301 https://emolo.cn$request_uri; # HTTPS到HTTPS的跳转
}
# 主HTTPS服务配置
server {
listen 443 ssl;
server_name emolo.cn; # 仅处理无www域名
# SSL配置保持不变...
# 移除原有的if判断(通过独立server块实现跳转)
# 优化缓存设置
location / {
root /usr/share/nginx/dist;
try_files $uri /index.html;
# 区分缓存策略
if ($uri ~* \.(?:css|js|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webp)$) {
add_header Cache-Control "public, max-age=2592000"; # 静态资源缓存30天
}
if ($uri ~* \.(?:html|json)$) {
add_header Cache-Control "no-cache"; # 动态内容不缓存
}
}
location / {
root /usr/share/nginx/dist;
add_header Cache-Control no-store always;
try_files $uri /index.html;
}
location ~ ^/(api|gravatar|feed\.xml) {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /websocket {
proxy_pass http://localhost:8080/websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header 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_set_header Origin "";
proxy_read_timeout 86400s;
proxy_send_timeout 60s;
proxy_connect_timeout 60s;
}
# 加载默认配置
include /etc/nginx/default.d/*.conf;
}