全球主机交流论坛

标题: http强制跳到 https带www nginx配置问题,大佬看下哪的问题 [打印本页]

作者: akige    时间: 2018-5-1 21:16
标题: http强制跳到 https带www nginx配置问题,大佬看下哪的问题
本帖最后由 akige 于 2018-5-1 21:20 编辑

配置如下 ssl的就不放出来了。没问题。
这样写看着没错啊?,就是跳不过去。

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

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

server {
    listen       443;
    server_name  aaa.com;
    return       301 https://www.aaa.com$request_uri;
}

我的需求就是
1.   xxx.com/a.html   跳到  https://www.xxx.com/a.html

2.   www.xxx.com/a.html   跳到  https://www.xxx.com/a.html

3.   https://xxx.com/a.html   跳到  https://www.xxx.com/a.html

完事。
作者: loti    时间: 2018-5-1 21:20
https://liyuans.com/archives/http-automatic-jump-https.html
看看这个
作者: akige    时间: 2018-5-1 21:23
loti 发表于 2018-5-1 21:20
https://liyuans.com/archives/http-automatic-jump-https.html
看看这个


http跳转到https我会。但是有这样一个问题。 aaa.com会跳转到 https://aaa.com  我需要统一https://www.aaa.com  类似百度那样
作者: march1993    时间: 2018-5-1 21:29
本帖最后由 march1993 于 2018-5-1 21:34 编辑

server_name 可以填多个,return 的时候会返回第一个
两个 if 没法合并,nginx 不支持
  1. listen 80 default_server default_server;
  2. listen 443 ssl default_server default_server;

  3. server_name www.xxx.com xxx.com yyy.com;
  4. if ($http_host != $server_name) {
  5.         return 301 https://$server_name$request_uri;
  6. }
  7. if ($scheme = http) {
  8.         return 301 https://$server_name$request_uri;
  9. }
复制代码

作者: sunzetu    时间: 2018-5-1 21:30
rewrite ^/(.*)$ https://www.xxx.com/$1 permanent;

作者: 陈道临    时间: 2018-5-1 21:42
server_name baidu.com www.baidu.com;
if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
if ($host != baidu.com) {  return 301 $scheme://baidu.com$request_uri;  }
www到no www

server_name www.baidu.com baidu.com;
if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
if ($host != www.baidu.com) {  return 301 $scheme://www.baidu.com$request_uri;
no www到www


分开写干嘛。。。
作者: march1993    时间: 2018-5-1 21:44
陈道临 发表于 2018-5-1 21:42
server_name baidu.com www.baidu.com;
if ($ssl_protocol = "") { return 301 https://$host$request_uri; ...

你这个如果是
  1. http://baidu.com
复制代码
会 301 两次的吧? 第一次到
  1. https://baidu.com
复制代码
第二次到
  1. https://www.baidu.com
复制代码

作者: akige    时间: 2018-5-1 21:56
march1993 发表于 2018-5-1 21:29
server_name 可以填多个,return 的时候会返回第一个
两个 if 没法合并,nginx 不支持
...




  1. server {
  2.         listen 80 ;
  3.        
  4.         listen 443 ssl http2;
  5.    
  6.    
  7.     index index.html index.htm index.php default.html default.htm default.php;
  8.        
  9.         server_name www.mysite.com mysite.com;
  10.        
  11.         if ($http_host != $server_name) {
  12.         return 301 https://$server_name$request_uri;
  13.         }
  14.         if ($scheme = http) {
  15.         return 301 https://$server_name$request_uri;
  16.         }
  17. }



  18. server
  19.     {
  20.         listen 80;
  21.         #listen [::]:80;
  22.         server_name www.mysite.com mysite.com;

  23.        # index index.html index.htm index.php default.html default.htm default.php;
  24.         root  /home/wwwroot/www.mysite.com;
  25.         
  26.                 include rewrite/laravel.conf;
  27.         #error_page   404   /404.html;

  28.         # Deny access to PHP files in specific directory
  29.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  30.         include enable-php-pathinfo.conf;

  31.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  32.         {
  33.             expires      30d;
  34.         }

  35.         location ~ .*\.(js|css)?$
  36.         {
  37.             expires      12h;
  38.         }

  39.         location ~ /.well-known {
  40.             allow all;
  41.         }

  42.         location ~ /\.
  43.         {
  44.             deny all;
  45.         }

  46.         access_log  /home/wwwlogs/www.mysite.com.log;
  47.     }


  48. server
  49.     {
  50.         listen 443 ssl http2;
  51.         #listen [::]:443 ssl http2;
  52.         server_name www.mysite.com ;
  53.         index index.html index.htm index.php default.html default.htm default.php;
  54.         root  /home/wwwroot/www.mysite.com;
  55.         ssl on;
  56.         ssl_certificate /usr/local/nginx/conf/ssl/www.mysite.com/fullchain.cer;
  57.         ssl_certificate_key /usr/local/nginx/conf/ssl/www.mysite.com/www.mysite.com.key;
  58.         ssl_session_timeout 5m;
  59.         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  60.         ssl_prefer_server_ciphers on;
  61.         ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
  62.         ssl_session_cache builtin:1000 shared:SSL:10m;
  63.         # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
  64.         ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

  65.         include rewrite/laravel.conf;
  66.         #error_page   404   /404.html;

  67.         # Deny access to PHP files in specific directory
  68.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  69.         include enable-php-pathinfo.conf;

  70.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  71.         {
  72.             expires      30d;
  73.         }

  74.         location ~ .*\.(js|css)?$
  75.         {
  76.             expires      12h;
  77.         }

  78.         location ~ /.well-known {
  79.             allow all;
  80.         }

  81.         location ~ /\.
  82.         {
  83.             deny all;
  84.         }

  85.         access_log  /home/wwwlogs/www.mysite.com.log;
  86.     }
复制代码


报错:

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
作者: 陈道临    时间: 2018-5-1 22:00
march1993 发表于 2018-5-1 21:44
你这个如果是   会 301 两次的吧? 第一次到  第二次到

分开用return 301 https://www.baidu.com$request_uri;也行啊
作者: akige    时间: 2018-5-1 22:03
陈道临 发表于 2018-5-1 21:42
server_name baidu.com www.baidu.com;
if ($ssl_protocol = "") { return 301 https://$host$request_uri; ...

这一段写在listen 80  443 端口都要加吗?
作者: march1993    时间: 2018-5-1 22:07
akige 发表于 2018-5-1 21:56
报错:

Firefox has detected that the server is redirecting the request for this address in a wa ...




  1. server {
  2.         listen 80 ;
  3.        
  4.         listen 443 ssl http2;
  5.         ssl on;
  6.         ssl_certificate /usr/local/nginx/conf/ssl/www.mysite.com/fullchain.cer;
  7.         ssl_certificate_key /usr/local/nginx/conf/ssl/www.mysite.com/www.mysite.com.key;
  8.         ssl_session_timeout 5m;
  9.         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  10.         ssl_prefer_server_ciphers on;
  11.         ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
  12.         ssl_session_cache builtin:1000 shared:SSL:10m;
  13.         # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
  14.         ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
  15.        
  16.         server_name www.mysite.com mysite.com;
  17.        
  18.         if ($http_host != $server_name) {
  19.                 return 301 https://$server_name$request_uri;
  20.         }
  21.         if ($scheme = http) {
  22.                 return 301 https://$server_name$request_uri;
  23.         }


  24.         index index.html index.htm index.php default.html default.htm default.php;
  25.         root  /home/wwwroot/www.mysite.com;
  26.        
  27.         include rewrite/laravel.conf;
  28.                
  29.         include enable-php-pathinfo.conf;

  30.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  31.         {
  32.                 expires      30d;
  33.         }

  34.         location ~ .*\.(js|css)?$
  35.         {
  36.                 expires      12h;
  37.         }

  38.         location ~ /.well-known {
  39.                 allow all;
  40.         }

  41.         location ~ /\.
  42.         {
  43.                 deny all;
  44.         }

  45.         access_log  /home/wwwlogs/www.mysite.com.log;
  46. }
复制代码

作者: 陈道临    时间: 2018-5-1 22:11
server {
  listen 80;
  listen [::]:80;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name www.baidu.com baidu.com;
  if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
  if ($host != www.baidu.com) {  return 301 $scheme://www.baidu.com$request_uri;

这样写一个server块就够了 看着舒服
作者: ponyxx    时间: 2018-5-1 22:12
为何不试试meta跳转?
作者: akige    时间: 2018-5-1 22:17
本帖最后由 akige 于 2018-5-1 22:19 编辑
march1993 发表于 2018-5-1 22:07


大佬 按照你的测试  还是不行:

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

nginx -s reload 有做
作者: akige    时间: 2018-5-1 22:21
ponyxx 发表于 2018-5-1 22:12
为何不试试meta跳转?

想在服务端做
作者: akige    时间: 2018-5-1 22:31
陈道临 发表于 2018-5-1 22:11
server {
  listen 80;
  listen [::]:80;

还是有问题。。大佬你测试过吗?
作者: akige    时间: 2018-5-1 22:32
陈道临 发表于 2018-5-1 22:11
server {
  listen 80;
  listen [::]:80;

能贴一个完整的。你测试过的吗?我看着你配的貌似是那个逻辑,但是测试确实不行。跟我之前写的情况有点类似。
作者: march1993    时间: 2018-5-1 22:34
akige 发表于 2018-5-1 22:32
能贴一个完整的。你测试过的吗?我看着你配的貌似是那个逻辑,但是测试确实不行。跟我之前写的情况有点类 ...

发个url来看看吧  
作者: akige    时间: 2018-5-1 22:45
陈道临 发表于 2018-5-1 22:11
server {
  listen 80;
  listen [::]:80;

  1. server
  2.         {
  3.                 listen 80;
  4.                 listen [::]:80;
  5.                 listen 443 ssl http2;
  6.                 listen [::]:443 ssl http2;
  7.                 server_name www.mysite.com mysite.com;
  8.                
  9.                 if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
  10.                 if ($host != www.mysite.com) {  return 301 $scheme://www.mysite.com$request_uri;}
  11.                
  12.                
  13.                
  14.         ssl on;
  15.         ssl_certificate /usr/local/nginx/conf/ssl/www.mysite.com/fullchain.cer;
  16.         ssl_certificate_key /usr/local/nginx/conf/ssl/www.mysite.com/www.mysite.com.key;
  17.         ssl_session_timeout 5m;
  18.         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  19.         ssl_prefer_server_ciphers on;
  20.         ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
  21.         ssl_session_cache builtin:1000 shared:SSL:10m;
  22.         # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
  23.         ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
  24.         
  25.         
  26.         index index.html index.htm index.php default.html default.htm default.php;
  27.                
  28.         root  /home/wwwroot/www.mysite.com;
  29.         
  30.         include rewrite/laravel.conf;
  31.                
  32.         include enable-php-pathinfo.conf;

  33.                
  34.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  35.         {
  36.                 expires      30d;
  37.         }

  38.         location ~ .*\.(js|css)?$
  39.         {
  40.                 expires      12h;
  41.         }

  42.         location ~ /.well-known {
  43.                 allow all;
  44.         }

  45.         location ~ /\.
  46.         {
  47.                 deny all;
  48.         }

  49.         access_log  /home/wwwlogs/www.mysite.com.log;
  50.         }
复制代码


大佬 按照你这个配置 真的有问题 。你去测试下。我看着语法也没问题。就是不行。。。。
作者: march1993    时间: 2018-5-1 22:52
akige 发表于 2018-5-1 22:45
大佬 按照你这个配置 真的有问题 。你去测试下。我看着语法也没问题。就是不行。。。。 ...


怎么感觉是你firefox的缓存 ,换个浏览器或者硬清空缓存试试看
还有 firefox的跳转记录也可以发上来
作者: guoaibing    时间: 2018-5-1 23:07
server
    {
        listen 80;
                server_name xxx.com www.xxx.com;
                return 301 https://www.xxx.com$request_uri;
    }
server
    {
        listen 443 ssl http2;
                if ($host = xxx.com) {
                return 301 https://www.xxx.com$request_uri;
    }

刚好最近也做了个一样跳转。。。完全没问题
作者: 陈道临    时间: 2018-5-1 23:07
akige 发表于 2018-5-1 22:45
大佬 按照你这个配置 真的有问题 。你去测试下。我看着语法也没问题。就是不行。。。。 ...


server {
  listen 80;
  listen [::]:80;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  #RSA
  ssl_certificate /root/ssl/strelizia.org_RSA.crt;
  ssl_certificate_key /root/ssl/strelizia.org_RSA.key;
  #ECC
  ssl_certificate /root/ssl/strelizia.org_ECC.crt;
  ssl_certificate_key /root/ssl/strelizia.org_ECC.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384HE-RSA-AES128-GCM-SHA256HE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHAHE-RSA-AES128-SHA256HE-RSA-AES128-SHAHE-RSA-AES256-SHA256HE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHAES-CBC3-SHA:!DSS;
  ssl_prefer_server_ciphers on;
  ssl_session_timeout 10m;
  ssl_session_cache builtin:1000 shared:SSL:10m;
  ssl_buffer_size 1400;
  add_header Strict-Transport-Security max-age=15768000;
  ssl_stapling on;
  ssl_stapling_verify on;
  server_name strelizia.org www.strelizia.org;
  if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
  if ($host != strelizia.org) {  return 301 $scheme://strelizia.org$request_uri;  }

对着看看 双证书部分无视即可
作者: march1993    时间: 2018-5-1 23:10
guoaibing 发表于 2018-5-1 23:07
server
    {
        listen 80;

我也是啊。。。我给的配置也是在用测试通过的。。。目测firefox缓存问题




欢迎光临 全球主机交流论坛 (https://loc.193.gs/) Powered by Discuz! X3.4