Redirect Nginx重定向到https://www.domain.tld

Redirect Nginx重定向到https://www.domain.tld,redirect,nginx,configuration,https,subdomain,Redirect,Nginx,Configuration,Https,Subdomain,我试图让我的域名只在前面有https://和www的情况下工作。不带www.的domain.com重定向到www是很重要的,https://始终处于启用状态也是很重要的。我很难做到这一点。我已经从配置中删除了所有重定向,因为它们都会给我错误 server { listen 80; default_type text/html; server_name epicmc.us; location / { root /usr/share/ngi

我试图让我的域名只在前面有https://和www的情况下工作。不带www.的domain.com重定向到www是很重要的,https://始终处于启用状态也是很重要的。我很难做到这一点。我已经从配置中删除了所有重定向,因为它们都会给我错误

    server {
    listen   80;
    default_type text/html; 
    server_name epicmc.us;

    location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
    }

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on the php-fpm socket
    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    }


    server {
    listen 443;
    default_type text/html;
    server_name www.epicmc.us;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    ssl on;
    ssl_session_timeout 5m;
    ssl_session_cache shared:SSL:5m;
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:-LOW:-SSLv2:-EXP:!kEDH:!aNULL;
    ssl_prefer_server_ciphers on;
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";

    ssl_certificate /etc/nginx/ssl/cert.crt;
    ssl_certificate_key /etc/nginx/ssl/private.key;

    location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    # proxy_pass http://127.0.0.1:8080;    
    #}

    error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    # root /usr/share/nginx/html;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    # # With php5-cgi alone:
    # fastcgi_pass 127.0.0.1:9000;
    # # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_index index.php;
    include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    # deny all;
    #}
    }
    }

编辑:我现在使用PHP重定向,但必须有一个更好的选项…

您应该定义额外的virtualhost,并将所有客户端重定向到所需的method+主机

添加到您的配置中(当然,根据您的喜好调整):


有两种方法,简单重定向返回301

    server {
    server_name www.example.com;
    listen         80;
    return 301 https://$host$request_uri;
}
或者使用重写规则,检查此问题的答案可能会有所帮助

 server {
        listen 80;
        server_name www.example.com ;
        location{
             rewrite ^(.*)$   https://www.example.com/$1 permanent;
         }
}

检查答案这可能会有帮助

嘿,伙计们,我正在使用Cloudflare的灵活SSL,所以我的问题是我必须在他们的站点上执行页面规则,而不是在我的配置中。这就是为什么我会出现重定向错误

 server {
        listen 80;
        server_name www.example.com ;
        location{
             rewrite ^(.*)$   https://www.example.com/$1 permanent;
         }
}