【问题标题】:Nginx redirection to https://www.domain.tldNginx 重定向到 https://www.domain.tld
【发布时间】:2015-02-14 01:42:47
【问题描述】:

我正在尝试让我的域名仅在其前面带有 https:// 和 www 的情况下工作。重要的是 domain.com 没有 www。重定向到 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 重定向,但必须有更好的选择...

【问题讨论】:

    标签: redirect nginx configuration https subdomain


    【解决方案1】:

    您应该定义额外的虚拟主机,并将所有客户端重定向到所需的方法+主机。

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

    # redirection vhost
    server {
        listen      10.1.2.3:80;
        server_name www.epicmc.us epicmc.us;
        access_log  /logs/access.log  full;
        error_log   /logs/error.log  notice;
        location / {
            rewrite  ^/(.*)$  https://www.epicmc.us/$1  permanent;
        }
    }
    

    【讨论】:

      【解决方案2】:

      有两种方法,简单的重定向return 301

          server {
          server_name www.example.com;
          listen         80;
          return 301 https://$host$request_uri;
      }
      

      或使用 rewrite 规则,检查此question 的答案可能会有所帮助

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

      检查这个question的答案可能会有所帮助

      【讨论】:

        【解决方案3】:

        大家好,我正在使用 Cloudflare 的灵活 SSL,所以我的问题是我必须在他们的网站上而不是在我的配置中执行页面规则。这就是我收到重定向错误的原因。

        【讨论】:

          猜你喜欢
          • 2017-11-05
          • 1970-01-01
          • 2020-09-11
          • 2020-03-21
          • 2016-03-10
          • 2015-08-16
          • 2019-06-04
          • 2019-08-16
          • 2015-05-09
          相关资源
          最近更新 更多