大网数据广告活动
服务器运维问答

nginx https auto redirect to specific port

· 52CE 我爱测

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 meudomain.com The first vhost needs to listen on 808…

问题原文

原帖链接 · c4rl · CC BY-SA 4.0

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 meudomain.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://meudomain.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 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 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;
    }
}

已采纳解答

原帖链接 · Daniel Gordi · CC BY-SA 4.0

Just add this server block to redirect all https://example.com to https://example.com:8080

server {
        listen 443 ssl;
        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
        return 301 https://example.com:8080$request_uri;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-10-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。