【问题标题】:How to redirect ip to https domain in nginx如何在 nginx 中将 ip 重定向到 https 域
【发布时间】:2020-05-15 12:58:43
【问题描述】:

我知道这里有很多类似的问题,但没有一个对我没有帮助,所以这是我的问题。

我需要将所有请求从我的服务器 ip 重定向到我的域。

我尝试了return 301 方法,它有点工作,但让我“重定向错误太多”。

最初不是我编写配置的,我害怕破坏它,它是一个实时服务器,所以我没有太多时间来测试。

这是我的配置:

    server {
    listen xxx.xxx.xxx.xxx:443 ssl;
    index index.php;
    server_name example.com;

    error_log  /var/log/nginx/error_example_com.log;
    access_log /var/log/nginx/access_example_com.log;
    root /var/www/prod/frontend/web;
    client_max_body_size 50m;

    ssl on;
    ssl_certificate /var/lib/dehydrated/certs/example.com/fullchain.pem;
    ssl_certificate_key /var/lib/dehydrated/certs/example.com/privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols SSLv2 SSLv3 TLSv1.2;
    ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:RSA+3DES:!NULL:!RC4;
    add_header Strict-Transport-Security "max-age=31536000";
    ssl_prefer_server_ciphers on;


    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location /images {
        alias /var/www/prod/frontend/web/images;
    }

    location /assets {
    expires 1d;
    }

    location /upload {
    expires 1d;
        alias /var/www/prod/frontend/web/uploads;
    }

    location /plugins/Global/scripts {
    alias /var/www/prod/frontend/web/js2;
    }

    location /plugins/Global/images {
        alias /var/www/prod/frontend/web/images;
    }

    location /plugins/Global/css {
    alias /var/www/prod/frontend/web/css;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_pass unix:/comn/php/php7.3-fpm.sock;
    }


    location /.well-known/acme-challenge {
        alias /var/lib/dehydrated/acme-challenges;
    }

}

server {
        listen 443 ssl;
        ssl_certificate /var/lib/dehydrated/certs/example.com/fullchain.pem;
    ssl_certificate_key /var/lib/dehydrated/certs/example.com/privkey.pem;
        server_name             www.example.com;


       location / {
                return 301 https://example.com$request_uri;
       }

        location ~ /.git/ {
                deny all;
        }
}


server {
    listen xxx.xxx.xxx.xxx:80;

    location / {
    return 301 https://example.com$request_uri;  # enforce https
    }

    location /.well-known/acme-challenge {
        alias /var/lib/dehydrated/acme-challenges;
    }
}

http://xxx.xxx.xxx.xxx 重定向到https://example.com 工作正常,但我不知道如何让https 重定向工作。我错过了什么?

这个站点还有一个管理面板,它的配置存储在另一个文件中,不确定是否也必须发布。

【问题讨论】:

  • 将第二个server 块设为默认值,这样任何不是example.comhttps 请求都将被重定向到example.com。将第二个 server 块中的 listen 语句从 listen 443 ssl; 更改为 listen 443 ssl default_server;
  • 您好,感谢您的回答!已经尝试过了,但似乎 nginx 忽略了默认服务器并继续使用第一个服务器块。

标签: nginx redirect https


【解决方案1】:

奇怪地在我的配置上添加了这个,甚至 default_server 都被忽略了..

server {
    listen 443 ssl http2;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

仍然不确定我的设置有什么问题,但至少现在可以正常工作。

nginx/1.14.2

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2021-03-25
    • 2019-07-21
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多