全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 4973|回复: 22

http强制跳到 https带www nginx配置问题,大佬看下哪的问题

[复制链接]
发表于 2018-5-1 21:16:04 | 显示全部楼层 |阅读模式
本帖最后由 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

完事。
发表于 2018-5-1 21:30:52 | 显示全部楼层
rewrite ^/(.*)$ https://www.xxx.com/$1 permanent;
发表于 2018-5-1 21:20:30 | 显示全部楼层
https://liyuans.com/archives/http-automatic-jump-https.html
看看这个
 楼主| 发表于 2018-5-1 21:23:46 | 显示全部楼层
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  类似百度那样
发表于 2018-5-1 21:29:05 | 显示全部楼层
本帖最后由 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. }
复制代码
发表于 2018-5-1 21:42:30 | 显示全部楼层
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


分开写干嘛。。。
发表于 2018-5-1 21:44:36 | 显示全部楼层
陈道临 发表于 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
复制代码
 楼主| 发表于 2018-5-1 21:56:37 | 显示全部楼层
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:09 | 显示全部楼层
march1993 发表于 2018-5-1 21:44
你这个如果是   会 301 两次的吧? 第一次到  第二次到

分开用return 301 https://www.baidu.com$request_uri;也行啊
 楼主| 发表于 2018-5-1 22:03:06 | 显示全部楼层
陈道临 发表于 2018-5-1 21:42
server_name baidu.com www.baidu.com;
if ($ssl_protocol = "") { return 301 https://$host$request_uri; ...

这一段写在listen 80  443 端口都要加吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-3-28 20:44 , Processed in 0.061698 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表