将 https 上的公共 IP 重定向到域 nginx

问题描述 投票:0回答:1

我正在使用nginx

我在 https 示例上配置了一个域 https://mywebsite.com

如果我尝试将其公共 IP 放在浏览器示例中https://10.1.2.1,它会给出消息连接不安全。

我想编写一个配置nginx,如果用户在浏览器上放置https://10.1.2.1,它总是会自动重定向它的域https://mywebsite.com

如何实现这一目标?

nginx
1个回答
0
投票

要实现自动重定向到您的域 https://mywebsite.com,您可以配置 NGINX。您可以在

/etc/nginx/sites-available/
中更新您网站的默认 nginx 配置文件。

  1. 打开默认的 NGINX 配置文件。
    sudo nano /etc/nginx/sites-available/default
    
  2. 添加以下配置。
    server {
        listen 443 ssl;
        server_name 10.1.2.1;  # Replace with your actual server's public IP
    
        # You can get the crt file from Certbot or other platforms such as Cloudflare and etc..
        ssl_certificate /etc/nginx/ssl/mywebsite.com.crt;  # Path to your SSL certificate
        ssl_certificate_key /etc/nginx/ssl/mywebsite.com.key; # Path to your SSL certificate key
    
        return 301 https://mywebsite.com$request_uri;
    }
    
    server {
        listen 80;
        server_name 10.1.2.1;  # Replace with your server's public IP
    
        return 301 https://mywebsite.com$request_uri;
    }
    
  3. 测试您的 NGINX 配置并重新加载 NGINX
    sudo nginx -t
    
    sudo systemctl reload nginx
    
© www.soinside.com 2019 - 2024. All rights reserved.