https auto redirect to specific port

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

https auto redirect to specific port

wld75
Hi experts,

I'm not sure if the subject is resuming correctly my question but I'll try
to explain it.

I have the configuration below in my server, this server has 2 vhosts:
example.com and mydomain.com

The first vhost needs to listen on 8080 (https) and as you can see I'm using
a redirect from http > https 8080. The second one is listening on 80.

My problem is that if a user type https in the address bar instead of http
it calls the second vhost.

How can redirect the https://example.com to https://example.com:8080 instead
of http://mydomain.com when a user type https in the address bar?

server {
    listen 80;
    server_name example.com;
    location '/.well-known/acme-challenge/' {
        autoindex on;
        root /var/www/certbot;
    }

    location / {
        if ($scheme = http) {
            return 301 <a href="https://example.com:8080;">https://example.com:8080;
        }
   }
}

server {
    listen 8080 default ssl;
    server_name example.com;
    ssl_certificate /etc/letsencrypt/live/example.com;/fullchain.pem; #
managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; #
managed by Certbot


    # logs
    error_log /var/log/nginx/example.com_error.log error;
    access_log /var/log/nginx/example.com_access.log;

    location / {
        index  index.html index.htm;
        autoindex on;
        proxy_pass <a href="http://internalserver:8080;">http://internalserver:8080;
        auth_basic      "Restricted area";
        auth_basic_user_file /srv/example.com/.htpasswd;
        client_body_temp_path /tmp 1 2;
        client_body_buffer_size 256k;
        client_body_in_file_only off;

    }


}

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,281566,281566#msg-281566

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: https auto redirect to specific port

Francis Daly
On Wed, Oct 10, 2018 at 01:56:47PM -0400, c4rl wrote:

Hi there,

if I am reading this right, then what you describe and what the config
file you provide say, are different.

> The first vhost needs to listen on 8080 (https) and as you can see I'm using
> a redirect from http > https 8080. The second one is listening on 80.
>
> My problem is that if a user type https in the address bar instead of http
> it calls the second vhost.

The config file says that if someone goes to http://example.com, you will
(almost always) redirect them to https://example.com:8080.

If someone goes to https://example.com, they will go to port 443. And
you have no listener for port 443 in the config file that you show.

> How can redirect the https://example.com to https://example.com:8080 instead
> of http://mydomain.com when a user type https in the address bar?

Add a 443 listener-server block that does ssl and redirects to
https://example.com:8080. Or, probably, change the current 443
listener-server block to redirect to https://example.com:8080 instead
of to http://example.com.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: https auto redirect to specific port

wld75
Wow, that's what I needed!  It's so simple.


Many thanks!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,281566,281568#msg-281568

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx