加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

node.js – nginx nodejs配置

发布时间:2020-12-13 21:33:33 所属栏目:Nginx 来源:网络整理
导读:我的当前nginx配置有问题.我想要做的是: 对于没有任何路径的请求,获取index.html(有效) 直接获取现有文件(有效) 如果请求的文件或路径实际上不存在,则向nodejs发送代理请求(404) 我在stackoverflow上尝试了几种配置,但它们都不符合我的需求. 这是我目前的配

我的当前nginx配置有问题.我想要做的是:

>对于没有任何路径的请求,获取index.html(有效)
>直接获取现有文件(有效)
>如果请求的文件或路径实际上不存在,则向nodejs发送代理请求(404)

我在stackoverflow上尝试了几种配置,但它们都不符合我的需求.

这是我目前的配置:

# IP which nodejs is running on
upstream app_x {
    server 127.0.0.1:3000;
}

# nginx server instance
server {
listen 80;
server_name x.x.x.x;
#access_log /var/log/nginx/x.log;

root /var/www/x/public;

location / {
    root /var/www/x/public;
    index index.html index.htm index.php;
}

location ^/(.*)${
    if (-f $request_filename) {
        break;
    }
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:3000;
}
}
最佳答案
我想我弄明白你要做什么.正确的方法是将try_files与命名位置一起使用.

尝试使用以下配置:

# IP which nodejs is running on
upstream app_x {
    server 127.0.0.1:3000;
}

# nginx server instance
server {
    listen 80;
    server_name x.x.x.x;
    #access_log /var/log/nginx/x.log;

    location / {
        root /var/www/x/public;
        index index.html index.htm index.php;
        try_files $uri $uri/ @node;
    }

    location @node {
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://app_x;
    }
}

注意:如果定义了上游,则应在proxy_ pass中使用该上游.此外,在代理时,始终添加X-Forwarded-For标头.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读