6

I have one nginx server setup(two config files) with two gunicorn web servers setup and running. One gunicorn is production and the other is staging.

I want nginx to serve http requests to xyz.com as well as https requests to xyz.com to the production gunicorn server @ 127.0.0.1:8000.

I have accomplished this with:

server {
   listen 80;
   server_name xyz.com;
   return 301 https://$http_host$request_uri;
}

server {
   listen 443 ssl;
   server xyz.com;
   ..... <<< ssl stuff
  location /{
      .... proxy_stuff
      proxy_pass http://127.0.0.1:8000;
  }
}

I also want http traffic to xyz.com:8080 and https traffic to xyz.com:8080 to hit the staging server @ 127.0.0.1:8081. I have been been able to get https traffic to xyz.com:8080 working as follows:

server {
   listen 8080 ssl;
   server_name xyz.com;
   ...... << ssl stuff
   location / {
      ...... << proxy stuff
      proxy_pass http://127.0.0.1:8081;
   }
}

But I can't seem to find a way to redirect http traffic at xyz.com:8080 to https traffic at xyz.com:8080. I have tried the same redirection that I did with port 80 but have not been successful.

Could use some direction.

bazfire
  • 63
  • 1
  • 1
  • 4
  • Do you want to listen for both http and https on the same port at the same time? If so that's not possible as far as I know. Or are you just trying to listen on http? – Tim Jan 19 '16 at 21:48
  • I want to listen to http requests and redirect them to https so I guess I do want to do the impossible especially since it is on the same port. Only other way I can think to redirect http from one port to another port as https. I did not want my users to have to be concerned if they are using http or https in typing the url but still always get https. – bazfire Jan 21 '16 at 18:27
  • Just run http on 8080 and https on 8081, forward from 8080 to 8081. Virtually no-one will type in those URLs or find them accidentally, so you could just run https on either port and send people who need it the link. – Tim Jan 21 '16 at 19:25

1 Answers1

8

Based on what you've said you want to listen for http and https on port 8080, which I don't believe is possible. Set up different server blocks for different ports, with the location block inside you can have the same proxy_pass to pass to wherever you like.

This is probably about the closest you can get to what you've said, which is listening on 8080 http, 8081 https, and forwarding from http to https. The rewrite might not be exactly right, but you get the idea.

server {
  listen 8080; # HTTP
  server_name example.com;
  rewrite ^ https://example.com:8081$request_uri? redirect;
  # rewrite ^ https://example.com:8081 redirect; # Alternate rewrite
}

server {
  listen 8081 ssl;
  server_name example.com;
  // ...... << ssl stuff
  location / {
    // ...... << proxy stuff to forward to http
    proxy_pass http://127.0.0.1:8080;
    // If you are not proxying to a service on the same server you can use the line below
    // proxy_pass http://example.com:8080; 
  }
}
Tim
  • 31,888
  • 7
  • 52
  • 78
  • Could you please explain this line: `proxy_pass http://127.0.0.1:8081;`? I needed to redirect http://localhost:8001 to https://localhost:8002 and this line worked `rewrite ^ https://localhost:8082$request_uri? redirect;` without writing `proxy_pass` line – AbhiNickz Aug 03 '17 at 10:25
  • @AbhiNickz suggest you ask a question if you need help, rather than comment on an old accepted answer. – Tim Aug 03 '17 at 18:50