/api/ → 后端8766端口/ws → 后端8766端口try_files $uri $uri/ /index.html; 正确❌ 缺少HTTP到HTTPS重定向
❌ 缺少非www到www的重定向
www.chengyantek.cnchengyantek.cn(没有www),没有处理⚠️ server_name只有www域名
当前问题:
http://www.chengyantek.cn 不会自动跳转到HTTPS修复方法: 将HTTP server块改为重定向:
# HTTP 端口 80 配置(重定向到HTTPS)
server {
listen 80;
server_name www.chengyantek.cn chengyantek.cn;
# 重定向所有HTTP请求到HTTPS
return 301 https://www.chengyantek.cn$request_uri;
}
当前问题:
www.chengyantek.cnhttps://chengyantek.cn,没有server块处理修复方法: 添加非www域名的HTTPS server块:
# 非www域名重定向到www(HTTPS)
server {
listen 443 ssl;
server_name chengyantek.cn;
# SSL证书配置(与非www域名相同)
ssl_certificate "/root/.acme.sh/chengyantek.cn_ecc/fullchain.cer";
ssl_certificate_key "/root/.acme.sh/chengyantek.cn_ecc/chengyantek.cn.key";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1h;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 重定向到www域名
return 301 https://www.chengyantek.cn$request_uri;
}
user root;
worker_processes auto;
events {
worker_connections 8192;
use epoll;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
# 启用gzip压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_comp_level 6;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/xml+rss
application/json;
# ==================== HTTP 端口 80 配置(重定向到HTTPS)====================
server {
listen 80;
listen [::]:80;
server_name www.chengyantek.cn chengyantek.cn;
# 🔥 重定向所有HTTP请求到HTTPS
return 301 https://www.chengyantek.cn$request_uri;
}
# ==================== HTTPS 端口 443 配置(主要配置)====================
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.chengyantek.cn;
# SSL证书配置
ssl_certificate "/root/.acme.sh/chengyantek.cn_ecc/fullchain.cer";
ssl_certificate_key "/root/.acme.sh/chengyantek.cn_ecc/chengyantek.cn.key";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1h;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 处理favicon.ico请求
location = /favicon.ico {
root /root/Lyrria/frontend/dist;
access_log off;
log_not_found off;
expires 1y;
add_header Cache-Control "public, immutable";
}
# 处理robots.txt请求
location = /robots.txt {
access_log off;
log_not_found off;
return 200 "User-agent: *\nDisallow:\n";
}
# WebSocket连接代理
location /ws {
proxy_pass http://127.0.0.1:8766;
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_read_timeout 3600s;
proxy_send_timeout 3600s;
proxy_connect_timeout 10s;
}
# API接口代理
location ^~ /api/ {
proxy_pass http://127.0.0.1:8766$request_uri;
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_read_timeout 60s;
proxy_send_timeout 60s;
proxy_connect_timeout 10s;
}
# 客户端指标上报接口代理
location = /metrics/client {
proxy_pass http://127.0.0.1:8766/metrics/client;
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_read_timeout 60s;
proxy_send_timeout 60s;
proxy_connect_timeout 10s;
}
# 静态资源缓存配置
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin '*' always;
add_header Access-Control-Allow-Methods 'GET, OPTIONS' always;
add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
add_header Access-Control-Max-Age 1728000 always;
add_header Content-Type 'text/plain; charset=utf-8' always;
add_header Content-Length 0 always;
return 204;
}
root /root/Lyrria/frontend/dist;
expires 1y;
add_header Cache-Control "public, immutable" always;
add_header X-Content-Type-Options nosniff always;
add_header Access-Control-Allow-Origin '*' always;
add_header Access-Control-Allow-Methods 'GET, OPTIONS' always;
}
# 前端静态文件服务
location / {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin '*' always;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
add_header Access-Control-Max-Age 1728000 always;
add_header Content-Type 'text/plain; charset=utf-8' always;
add_header Content-Length 0 always;
return 204;
}
root /root/Lyrria/frontend/dist;
try_files $uri $uri/ /index.html;
# 安全头设置
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# CORS 响应头
add_header Access-Control-Allow-Origin '*' always;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
# HTML文件不缓存
location ~* \.html$ {
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";
}
}
}
# ==================== 非www域名重定向到www(HTTPS)====================
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name chengyantek.cn;
# SSL证书配置
ssl_certificate "/root/.acme.sh/chengyantek.cn_ecc/fullchain.cer";
ssl_certificate_key "/root/.acme.sh/chengyantek.cn_ecc/chengyantek.cn.key";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1h;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 🔥 重定向到www域名
return 301 https://www.chengyantek.cn$request_uri;
}
}
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
将HTTP server块改为重定向:
# HTTP 端口 80 配置(重定向到HTTPS)
server {
listen 80;
listen [::]:80;
server_name www.chengyantek.cn chengyantek.cn;
# 重定向所有HTTP请求到HTTPS
return 301 https://www.chengyantek.cn$request_uri;
}
在HTTPS server块后面添加:
# 非www域名重定向到www(HTTPS)
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name chengyantek.cn;
# SSL证书配置
ssl_certificate "/root/.acme.sh/chengyantek.cn_ecc/fullchain.cer";
ssl_certificate_key "/root/.acme.sh/chengyantek.cn_ecc/chengyantek.cn.key";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1h;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 重定向到www域名
return 301 https://www.chengyantek.cn$request_uri;
}
# 测试nginx配置语法
nginx -t
# 如果测试通过,重载配置
nginx -s reload
# 或
systemctl reload nginx
# 测试HTTP到HTTPS重定向
curl -I http://www.chengyantek.cn/share
# 应该返回 301 重定向到 https://www.chengyantek.cn/share
# 测试非www到www重定向
curl -I https://chengyantek.cn/share
# 应该返回 301 重定向到 https://www.chengyantek.cn/share
http://www.chengyantek.cn → 不会自动跳转到HTTPShttps://chengyantek.cn → 可能返回404或默认页面http://www.chengyantek.cn → 自动重定向到 https://www.chengyantek.cnhttp://chengyantek.cn → 自动重定向到 https://www.chengyantek.cnhttps://chengyantek.cn → 自动重定向到 https://www.chengyantek.cnhttps://www.chengyantek.cnserver {
listen 80;
server_name www.chengyantek.cn chengyantek.cn;
return 301 https://www.chengyantek.cn$request_uri; # 添加重定向
}
server {
listen 443 ssl;
server_name chengyantek.cn;
# ... SSL配置 ...
return 301 https://www.chengyantek.cn$request_uri; # 添加重定向
}
nginx -t 测试当前配置的优点:
需要修复的问题:
修复后:
https://www.chengyantek.cn修复后,微信JSSDK的URL不匹配问题应该会解决!