ymlupstream songxuan {
server 192.168.201.100:8081 weight=5;
server 192.168.201.101:8082;
}
server {
listen 81;
server_name localhost;
# 精确匹配 /test
location = /test {
rewrite ^/test$ /demo permanent;
proxy_pass http://songxuan;
}
# 匹配 /test/ 开头的路径
location /test/ {
rewrite ^/test/(.*)$ /demo/$1 break;
proxy_pass http://songxuan;
}
location / {
root html;
index index.html index.htm;
proxy_pass http://songxuan;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
写了两个匹配规则:
第一个匹配规则:访问 http://localhost:81/test 会重定向到 http://localhost:81//demo
第二个匹配规则:访问 http://localhost:81/test/xxx 会转发到 http://localhost:81/demo/xxx
使用 permanent 会返回301永久重定向,浏览器地址栏会显示变化
我要直接把自己的这个路径:https://songxuan.vip/category
转成这个路径:https://songxuan.vip/tag
yml# songxuan.conf 完整配置
upstream songxuan {
server 117.72.79.70:81;
}
# HTTP 服务 - 自动跳转到 HTTPS
server {
listen 80;
server_name songxuan.vip; # 修改为您的域名
# 所有HTTP请求重定向到HTTPS
return 301 https://$host$request_uri;
}
# HTTPS 服务
server {
listen 443 ssl;
server_name songxuan.vip; # 修改为您的域名
# SSL证书配置 - 使用您现有的证书文件
ssl_certificate /usr/local/nginx/conf/cert/songxuan.vip.pem; # 根据您的实际路径
ssl_certificate_key /usr/local/nginx/conf/cert/songxuan.vip.key; # 根据您的实际路径
# SSL协议优化
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on;
# 精确匹配 /test
location = /category {
rewrite ^/category$ /tag permanent;
proxy_pass http://songxuan;
}
# 保留原有location配置
location / {
root /usr/local/nginx/html;
index guoqin.html;
proxy_pass http://songxuan; # 按需启用
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
error_page 404 /guoqin.html;
location = /guoqin.html {
root /usr/local/nginx/html;
}
}
本文作者:松轩(^U^)
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!