5

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 files directly (works)
  • If the requested file or path does not physically exist, proxy request to nodejs (404)

I have tried several configurations found here on stackoverflow, but none of them fit my needs.

Here is my current 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;

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;
}
}
4
  • Just remove if (-f $request_filename) statement and the break? I don't really get what's working and what's not, and what your needs are. Commented Dec 21, 2013 at 7:01
  • Is there a reason why you aren't serving index.html with node? That would make things way easier. Commented Dec 21, 2013 at 7:07
  • The reason why I'm not serving index.html with nodejs is, that it is static html. The project is an ajax driven application and I'm trying to serve nothing but json with nodejs. Commented Dec 21, 2013 at 9:55
  • what's the use of the upstream if you're not using it afterwards ? Commented Apr 30, 2016 at 9:30

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I was forgetting to first define the "root" directive. Was including the entire path with $uri at the end (Incorrect: /var/www/x/public/$uri).
1

I was wondering that problem is in application path. Please find the following code excerpt from toontuts blog for full configuration of nginx with nodejs, you may find this link

upstream subdomain.your_domain.com {
  server 127.0.0.1:3000;
}
 
server {
  listen 0.0.0.0:80;
  server_name subdomain.your_domain.com;
  access_log /var/log/nginx/subdomain.your_domain.access.log;
  error_log /var/log/nginx/subdomain.your_domain.error.log debug;
 
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
 
    proxy_pass http://subdomain.your_domain.com;
    proxy_redirect off;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.