V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
gotonull
V2EX  ›  NGINX

nginx 配置问题求解答

  •  
  •   gotonull · 25 天前 · 807 次点击

    问题描述:a 服务器的 nginx 服务会将 /aisp/approval/ 路径的请求转发给我,我收到请求后根据user-agent判断是移动端还是 pc 端请求,如果为移动端的,则去当前服务器的/home/centos/web/h5目录下响应对应的静态文件。如果是/aisp/approval/api/路径下的请求则请求后端服务,当前的配置:

    server {
            listen 9999;
            server_name localhost;
    
            location /aisp/approval/ {
                if ($is_mobile = 0) {
                    rewrite ^/aisp/approval/(.*)$ https://b.com/$1 permanent;
                }
                alias /home/centos/web/h5/;
                try_files $uri $uri/ /index.html;
            }
    
            location  /aisp/approval/api/ {
                limit_conn perip 1000;
                proxy_pass http://10.156.166.23:9088/;
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                client_max_body_size 600M;
                client_body_buffer_size 512k;
            }
        }
    
    

    现在的问题是通过移动端请求a.com/aisp/approval/workOrder/orderHandlerDetails,error.log 会报:

    open() "/usr/local/nginx/html/home/centos/supervision/web/h5/index.html" failed (2: No such file or directory)
    

    /usr/local/nginx/html 是我 nginx 的安装目录,我要如何配置才能在请求 a.com/aisp/approval/workOrder/时响应/home/centos/supervision/web/h5/目录下的 index.html

    3 条回复    2024-12-06 12:27:02 +08:00
    kingwrcy
        1
    kingwrcy  
       25 天前
    server {
    listen 9999;
    server_name localhost;

    location /aisp/approval/ {
    if ($is_mobile = 0) {
    rewrite ^/aisp/approval/(.*)$ https://b.com/$1 permanent;
    }
    root /home/centos/web/h5; # 使用 root 指令
    try_files $uri $uri/ /aisp/approval/index.html; # 确保指向正确的 index.html
    }

    location /aisp/approval/api/ {
    limit_conn perip 1000;
    proxy_pass http://10.156.166.23:9088/;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    client_max_body_size 600M;
    client_body_buffer_size 512k;
    }
    }
    yippee0539
        2
    yippee0539  
       24 天前
    啥了,想了半天没看到没有指定 root
    楼上 nb
    gotonull
        3
    gotonull  
    OP
       24 天前
    @kingwrcy 可是用 root 的话,我就必须要在/home/centos/web/h5 目录下建 2 级/aisp/approval 目录才能正确访问到静态资源,我用 alias 就是为了不建这 2 级目录
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1133 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:21 · PVG 02:21 · LAX 10:21 · JFK 13:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.