Nginx服务器:重定向www,ip和非ssl

时间:2017-10-30 18:08:59

标签: ssl nginx url-redirection no-www

我一直在努力使用我的Nginx服务器的.conf文件。尝试重定向这些网址时,我收到重定向循环错误:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

# 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服务器。此外,我使用本教程设置服务器和配置文件:https://code.lengstorf.com/deploy-nodejs-ssl-digitalocean/#enable-nginx

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