2

I'm a beginner Nginx user trying to proxy the following:

http://subdomain.example.com/mypage/login
TO
http://some_ip_address/login

(not just /login - the site has other contexts too e.g. /static, /api, etc.)

Whilst I can functionally make this work, the user sees http://some_ip_address in their browser, which I'd like to avoid.

That config looks like this:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout  3;
    server {
        server_name this_server_ip;
        location /mypage/ {
            proxy_pass http://some_ip_address;
            rewrite ^/mypage/(.*)$ http://some_ip_address/$1 last;
        }
        location / {
          root /var/local/directory/;
        }
    }
}

To try to fix it, I've tried combinations of:

  • proxy_pass http://some_ip_address/; (i.e. with trailing slash)
  • proxy_set_header Host $host;
  • rewrite ^/mypage/(.*)$ /$1 last;

But I either get 404's or serve up the page that's hosted at http://subdomain.example.com, i.e. rewrite works but proxy_pass doesn't.

There are a few similar questions on serverfault but none seem to address my particular flavour of this problem, unfortunately. Examples are this one and that one.

Any suggestions would be very much appreciated, thank you.

2
  • Why are you doing both proxy_pass and rewrite inside the location? You shouldn't need the rewrite here at all, since the proxy_pass kind of does the rewrite already. What does the logfile of http://some_ip_address tell you? Jul 16, 2015 at 21:39
  • Without rewrite, Nginx tries to fetch a local resource, i.e. not from some_ip_address: open() "/var/local/directory/login" failed (2: No such file or directory)
    – Paul J
    Jul 17, 2015 at 20:46

1 Answer 1

1

You are specifying this_server_ip as the hostname for this virtualhost. That means that this virtualhost isn't used for any requests that have a domain name, but your nginx default virtual server is used for requests for http://subdomain.example.com/.

You need to change your server_name to subdomain.example.com, or add default_server to the listen directive on this server block and remove it from the nginx default configuration.

EDIT: Try this vhost configuration:

server {
    server_name subdomain.example.com;
    location ~ /(mypage|static/api)/ {
        proxy_pass http://some_ip_address/$1/;
    }
    location / {
      root /var/local/directory/;
    }
}

That is, you don't need a rewrite statement at all, because you are proxying the request to another server.

2
  • That doesn't seem to do the trick unfortunately. Thanks for the suggestion though (and sorry it took me a little while to test it). To be clear, I used server { server_name subdomain.example.com; listen 80 default_server; ... }. I tried both with and without proxy_pass on my location, and also tried both server_name, listen and one or the other...
    – Paul J
    Jul 29, 2015 at 20:31
  • I changed the configuration, I had wrong idea here. Jul 30, 2015 at 10:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .