【问题标题】:Nginx server : Redirecting www, ip and non-sslNginx 服务器:重定向 www、ip 和非 ssl
【发布时间】:2018-04-11 19:26:17
【问题描述】:

我一直在努力处理我的 Nginx 服务器的 .conf 文件。我在尝试重定向这些 url 时遇到重定向循环错误:

http://example.com
http://www.example.com
https://www.example.com
http://11.111.11.11
https://11.111.11.11

收件人:https://example.com

所以我要做的是将每个非 ssl url、www 前缀 url 和我的服务器的 ip 地址重定向到我的域名。 这是我的代码:

# redirect ip to domain name
server {
    listen               80;
    listen               443 ssl;
    server_name          11.111.11.11; #server_ip
    ssl_certificate      /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/mydomain.com/privkey.pem;

    return 301 $scheme://mydomain.com$request_uri;
}

# HTTP — redirect all traffic to HTTPS
server {
    listen               80;
    listen               443 ssl;
    server_name          www.mydomain.com;
    ssl_certificate      /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/mydomain.com/privkey.pem;

    return 301 $scheme://mydomain.com$request_uri;
}

# HTTPS — proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mydomain.com;

    # Use the Let’s Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:5000/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }
}

【问题讨论】:

标签: ssl nginx url-redirection no-www


【解决方案1】:

好的,这几天我在网上搜索了一下,似乎下面的解决方案有效:

# HTTP — redirect all traffic to HTTPS
server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    server_name www.example.com 00.000.00.00; # www and your ip address
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    return 301 https://example.com$request_uri;
}

# HTTPS — proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;

    # Use the Let’s Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:5000/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }
}

为了更详细地记录这一点,我试图将 nginx 服务器代理到端口 5000 上的 nodejs 服务器。此外,我使用本教程来设置服务器和 conf 文件:https://code.lengstorf.com/deploy-nodejs-ssl-digitalocean/#enable-nginx

希望这会对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2016-04-02
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多