Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX => serve several applications on a single host name with sub-uris

Tags:

nginx

I'd like to serve several applications from the same server, reversed-proxied through nginx. I'd like these applications to be available through a single domain name with sub-uris.

e.g.

www.mydomain.com/nodejs => caught by nginx listening to port 80 and served through to a node.js app running on port 3001

www.mydomain.com/rails => caught by nginx listening to port 80 and served through to a rails app running on port 3002

My first stab is to start with two upstreams:

# /etc/nginx/sites-available/mydomain.com

upstream nodejs {
  server 127.0.0.1:3001;
}

upstream rails {
  server 127.0.0.1:3002;
}

server {
  listen 80 default deferred;

  # What do I put here so that
  # mydomain.com/nodejs is proxied to the nodejs upstream and
  # mydomain.com/rails  is proxied to the rails  upstream ???
}

Does anyone know this or point me in the right direction?

like image 375
olivoil Avatar asked Apr 17 '12 19:04

olivoil


People also ask

What is $host in nginx?

$http_host equals always the HTTP_HOST request header. $host equals $http_host , lowercase and without the port number (if present), except when HTTP_HOST is absent or is an empty value. In that case, $host equals the value of the server_name directive of the server which processed the request.

What is Sendfile Nginx?

By default, NGINX handles file transmission itself and copies the file into the buffer before sending it. Enabling the sendfile directive eliminates the step of copying the data into the buffer and enables direct copying data from one file descriptor to another.

What is Default_server Nginx?

In the Nginx configuration file, the default_server option specifies the default server to which a client request with an unknown domain and an empty host field will be forwarded.

What is Proxy_pass Nginx?

The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.


1 Answers

How about:

upstream nodejs {
    server 127.0.0.1:3001;
}

upstream rails {
    server 127.0.0.1:3002;
}

server {
    listen 80;  

    location /nodejs {
        proxy_pass         http://nodejs;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

    location /rails {
        proxy_pass         http://rails;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

}

or shortly:

server {   
    listen 80;     

    location /nodejs {
        proxy_pass         http://127.0.0.1:3001;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

    location /rails {
        proxy_pass         http://127.0.0.1:3002;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

}

?

Most of the proxy directives are optional (you probably just need proxy_pass and proxy_redirect) but useful.

like image 116
kgr Avatar answered Oct 24 '22 21:10

kgr