nginx + nodejs configuration

前端 未结 2 1713
感情败类
感情败类 2021-02-06 12:32

I have a problem with my current nginx configuration. What I am trying to do is:

  • For requests without any path, get the index.html (works)
  • Get existing fi
2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 13:02

    I think I figured out what you were trying to do. The proper way is to use try_files together with a named location.

    Try with the following configuration:

    # 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;
        }
    }
    

    Note: When you have an upstream defined you should use that in your proxy_ pass. Also, when proxying, always add the X-Forwarded-For header.

提交回复
热议问题