13

I am trying to configure nginx as a reverse proxy for multiple servers on my LAN. They should go out on my WAN with different subdomains. My configuration looks like this:

@ReverseProxy:/etc/nginx/sites-enabled$ cat reverseproxy 
server { 
  server_name DOMAIN.eu;

  # app1 reverse proxy follow
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://10.0.2.5:80;

}

server { 
  server_name Subdomain.domain.eu;

  # app2 reverse proxy settings follow
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://10.0.2.33:80;
}

But I am getting this error and can't get any further....

@ReverseProxy:/etc/nginx/sites-enabled$ nginx -t
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2009/01/04 12:22:13 [warn] 1302#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
2009/01/04 12:22:13 [emerg] 1302#0: "proxy_pass" directive is not allowed here in /etc/nginx/sites-enabled/reverseproxy:8

nginx: configuration file /etc/nginx/nginx.conf test failed

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58

2 Answers2

14

Your problem is that you are using proxy_pass inside server block, which is not allowed. Try using:

server {
    server_name Subdomain.domain.eu;

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

inside your server block. Proxy options cannot be set on server level, as nginx documentation tells.

The other problems in your log happen because you have somehow your nginx starting up as a regular user, although it is supposed to start up as root.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
12

This thread solved my problem, but I thought it would be useful for others to have a completed configuration to see. The following configuration will reverse proxy for hostnames app1.local and app2.local, where app1 gets forwarded to another application listening on port 3300 and app2 is forwarded to a different application listening on port 3000. It is in a file here /etc/nginx/sites-available/two-applications.conf

server {
  server_name app1.local;

  location ~ {
    proxy_pass_header Authorization;
    proxy_pass http://localhost:3300;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_buffering off;
    client_max_body_size 0;
    proxy_read_timeout 36000s;
    proxy_redirect off;
  }
}

server {
  server_name app2.local;

  location ~ {
    proxy_pass_header Authorization;
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_buffering off; 
    client_max_body_size 0;
    proxy_read_timeout 36000s;
    proxy_redirect off;
  }
}

Also, those hostnames are made up and need to be in /etc/hosts as follows to have them work:

127.0.0.1       app1.local
127.0.0.1       app2.local

For the sake of completeness (as setup on Ubuntu Linux), this file lives in /etc/nginx/sites-available/two-applications.conf and is symlinked into /etc/nginx/sites-enabled/two-applications.conf The filename and symlink name can be anything of course. After updating that running sudo service nginx reload will pick up the change.

user605331
  • 223
  • 3
  • 6
  • Is it correct that if I had two different machines running on the same port - say two web servers on separate machines, all I would have to change in the code is the IP address in /etc/hosts? For example: 192.168.0.101 app1.local and 192.168.0.102 app2.local? –  Jan 24 '17 at 05:59
  • 2
    For the example you're describing, it doesn't sound like you need any nginx configuration at all. The configuration discussed in this thread solves a different problem. You can probably just use your hosts file and that'll do it. If you want to run everything though nginx though, you'll need to set the hosts file entry and also change the proxy_pass line to use something other than localhost, as it sounds like for your case you need a different machine listed there. – user605331 Jan 24 '17 at 15:19