Discussion:
Redirect http:8080 to https:8443
ST
2017-06-05 12:28:09 UTC
Permalink
Hello,

I try to redirect http on port 8080 to https on port 8443 as follows,
but it doesn't seem to work. http redirects to https, but the port
remains the same - 8080. Why?

Thank you!

# redirect http to https
server {
listen 8080;
server_name n.example.com;
return 301 https://$host:8443/$request_uri;
}

server {
listen 8443 ssl;
server_name n.example.com;
...
}
Jim Ohlstein
2017-06-05 12:43:17 UTC
Permalink
Post by ST
Hello,
I try to redirect http on port 8080 to https on port 8443 as follows,
but it doesn't seem to work. http redirects to https, but the port
remains the same - 8080. Why?
Thank you!
# redirect http to https
server {
    listen 8080;
    server_name n.example.com;
    return 301 https://$host:8443/$request_uri;
}
server {
        listen   8443 ssl;
server_name n.example.com;
...
}
This is _just a guess_, but "$host" may contain the port.

Try using:

return 301 https://n.example.com:8443$request_uri;
--
Jim Ohlstein
Professional Mailman Hosting
https://mailman-hosting.com/
Miguel C
2017-06-05 12:48:28 UTC
Permalink
That should work, whats the output you get using curl or httpie.

the same config works fine for me:

$ curl -I http://127.0.0.1:8080

HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.0 (Ubuntu)
Date: Mon, 05 Jun 2017 12:45:13 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: https://127.0.0.1:8443/ <---------------------------------------

$ http -h http://127.0.0.1:8080
HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Content-Length: 194
Content-Type: text/html
Date: Mon, 05 Jun 2017 12:45:22 GMT
Location: https://127.0.0.1:8443/ <--------------------------------------
Server: nginx/1.10.0 (Ubuntu)
ST
2017-06-05 13:01:17 UTC
Permalink
Thank you for the fast response.
curl shows correct output, like yours, however in Chromium/Firefox only
http redirects to https while port remains the same - 8080, so I get the
error: "This site can’t provide a secure connection"

If I clear cache it works sometimes...
Post by Miguel C
That should work, whats the output you get using curl or httpie.
$ curl -I http://127.0.0.1:8080
HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.0 (Ubuntu)
Date: Mon, 05 Jun 2017 12:45:13 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: https://127.0.0.1:8443/
<---------------------------------------
$ http -h http://127.0.0.1:8080
HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Content-Length: 194
Content-Type: text/html
Date: Mon, 05 Jun 2017 12:45:22 GMT
Location: https://127.0.0.1:8443/
<--------------------------------------
Server: nginx/1.10.0 (Ubuntu)
_______________________________________________
nginx mailing list
http://mailman.nginx.org/mailman/listinfo/nginx
b***@jacekowski.org
2017-06-05 13:16:50 UTC
Permalink
Post by ST
If I clear cache it works sometimes...
Post by Miguel C
$ curl -I http://127.0.0.1:8080
HTTP/1.1 301 Moved Permanently
Browsers may choose to aggresively cache 301 Moved Permanently responses.
302 Found is strongly preferred for testing. You may want to choose to stick
with 302 for production use as well.

Your browsers may have cached 301 already.

https://stackoverflow.com/a/21396547
http://getluky.net/2010/12/14/301-redirects-cannot-be-undon/

Best regards,
BlessJah
ST
2017-06-05 14:13:56 UTC
Permalink
Thank you very much! This solved the issue!
Post by b***@jacekowski.org
Post by ST
If I clear cache it works sometimes...
Post by Miguel C
$ curl -I http://127.0.0.1:8080
HTTP/1.1 301 Moved Permanently
Browsers may choose to aggresively cache 301 Moved Permanently responses.
302 Found is strongly preferred for testing. You may want to choose to stick
with 302 for production use as well.
Your browsers may have cached 301 already.
https://stackoverflow.com/a/21396547
http://getluky.net/2010/12/14/301-redirects-cannot-be-undon/
Best regards,
BlessJah
_______________________________________________
nginx mailing list
http://mailman.nginx.org/mailman/listinfo/nginx
Loading...