nginx配置更改无法找到的请求(API路径)代理到后端方案
This commit is contained in:
31
ngnix.conf
31
ngnix.conf
@@ -1,4 +1,4 @@
|
||||
# Nginx 静态文件服务 + 自定义 404 页面
|
||||
# Nginx 静态文件服务 + 智能代理 + 自定义 404 页面
|
||||
|
||||
# HTTP 重定向到 HTTPS
|
||||
server {
|
||||
@@ -19,7 +19,7 @@ server {
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers HIGH:!aNULL:!MD5;
|
||||
|
||||
# 使用项目自定义静态 404 页面,保持 nginx 返回 404 状态码
|
||||
# 自定义 404 页面,保持 404 状态码
|
||||
error_page 404 /404.html;
|
||||
|
||||
location = /404.html {
|
||||
@@ -43,17 +43,36 @@ server {
|
||||
# 网页端(public/web/index.html -> dist/web/index.html)
|
||||
location /web/ {
|
||||
alias /usr/share/nginx/html/web/;
|
||||
try_files $uri $uri/ =404;
|
||||
try_files $uri $uri/ /web/index.html;
|
||||
}
|
||||
|
||||
# 后台管理端(dist/index.html,前缀 /sys/)
|
||||
location /sys/ {
|
||||
alias /usr/share/nginx/html/;
|
||||
try_files $uri $uri/ =404;
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
# 其他静态资源或任意路径,未命中时统一交给 nginx 404,再由自定义 404 页面展示
|
||||
# 1. 先尝试作为静态文件查找
|
||||
location / {
|
||||
try_files $uri $uri/ =404;
|
||||
try_files $uri $uri/ @proxy;
|
||||
}
|
||||
|
||||
# 2. 无法找到的请求(API路径)代理到后端
|
||||
location @proxy {
|
||||
# 判断 URI 最后一段是否有扩展名
|
||||
# 有扩展名返回 404,无扩展名则代理
|
||||
if ($uri ~ \.[^./]+$) {
|
||||
return 404;
|
||||
}
|
||||
|
||||
proxy_pass http://116.204.74.41:8000;
|
||||
proxy_http_version 1.1;
|
||||
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_connect_timeout 30s;
|
||||
proxy_send_timeout 30s;
|
||||
proxy_read_timeout 30s;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user