0

I am trying to configure nginx but I cannot get it to work despite of my multiple attempts. What I need is:

  • port 80, /download is served by nginx
  • port 80, anything else is redirected to the same machine, port 8080

This works fine, but now I need that only in the case of the root (http://myhost/) the client is redirected to the default app at /Games. My current configuration is wrong and redirects me in an infinite loop. I got some ideas from here but couldn't make it work. Examples of the redirections:

ex1: http://myhost/  --> http://myhost/Games --> http://localhost:8080/Games
ex2: http://myhost/Books --> http://localhost:8080/Books

I have tried

        location / {
            proxy_pass  http://localhost:8080/Games;
        }

But this seems to act on everything (ex: /Books, /XYZ, ...). I think a redirection when the exact root is specified is the cleanest.

Also, I need to replicate this on the HTTPS. I suppose this will also work over the "stream" element.


nginx.conf:

http {
    server {
        listen  80;
        root /home/www/;
        location = / {
            return 301 http://$host/Games;
        }
        location / {
            proxy_pass  http://localhost:8080;
        }
        location /download/ {
            ....
        }
    }
}

stream {
    server {
        listen  443;
        ...
    }   
} 
user1156544
  • 127
  • 6

1 Answers1

0

What exactly is the question here? Your configuration works for me:

server {
    listen 80;
    listen [::]:80;

    server_name 192.168.0.2;
    root /var/www;

    location = / {
        return 301 http://$host/folder;
    }

    location / {
        proxy_pass http://otherserver:80;
    }
}

Logs:

[22/Feb/2018:17:04:55 +0100] "GET / HTTP/1.1" 301
[22/Feb/2018:17:04:55 +0100] "GET /folder HTTP/1.1" 301
[22/Feb/2018:17:04:55 +0100] "GET /folder/ HTTP/1.1" 200
[22/Feb/2018:17:11:29 +0100] "GET /index.html HTTP/1.0" 200

What do you see in your logs? Do requests fail to be proxied at all or does the localhost cause the issue? What does your localhost's config look like? There's too little information to be able to help you.

Also, I need to replicate this on the HTTPS. I suppose this will also work over the "stream" element.

Not sure what you mean, but you can just add another serverblock with the same config with listen 443 instead of listen 80 and add the ssl directives.

cetteup
  • 166
  • 5
  • The redirection works. The error I see is "This page isn’t working myhost redirected you too many times". I suppose I am redirected, then instead of entering the 2nd location block, I enter the 1st one again, and again, and again.... Infinite loop as I said. Also, the 443 directive is OK but I need to use stream to provide the HTTPS outside nginx in all cases except the "/download" – user1156544 Feb 22 '18 at 16:54
  • Have you checked your logs or tested whether your config works if you use rewrite in the first block rather than return? – cetteup Feb 22 '18 at 17:02
  • After a lot of testing and moving things around the redirection seems to work with the directives above. However the HTTPS is not working but I think I will have to open a new question since it looks like a different problem – user1156544 Feb 22 '18 at 18:12