大网数据广告活动
服务器运维问答

403 error; won't proxy: NginX and Node.js

· 52CE 我爱测

I currently have a setup where all static files are served by NginX, and if no static file is found it is moved onto a node.js server. Unfortunately I am getting a 403 error for the root request. Everything else is working fine. …

问题原文

原帖链接 · Gauss · CC BY-SA 3.0

I currently have a setup where all static files are served by NginX, and if no static file is found it is moved onto a node.js server. Unfortunately I am getting a 403 error for the root request. Everything else is working fine.

server {
    listen *:80;
    error_page 404 = /404.html;
    root /web/sites/this.site.tld/static;
    index home;

    location / {
        try_files $uri $uri/ @proxy;
    }

    location @proxy {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_X_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://thisSiteApp;
        proxy_redirect off;
    }
}

upstream thisSiteApp{
    server 127.0.0.1:3000;
}

I am fairly sure the problem lies with the index directive. Placing a "index.html" file in static and changing the directive to "index index.html" works. But what I really want to do is make it look to the node server for the index location, while still serving my static files without looking at node.

NB. If I put the proxy settings under the / location the node server serves the root just fine (well without the static files, but it serves its part fine).

社区高分解答

原帖链接 · 89c3b1b8-b1ae-11e6-b842-48d705 · CC BY-SA 3.0

Your try_files directive has a forward slash, so nginx will try to serve the directory when just going to the bare root of example.org/.

Instead of:

location / {
    try_files $uri $uri/ @proxy;
}

use:

location / {
    try_files $uri @proxy;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-07-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。