“Nginx http重定向到https”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
 
(未显示同一用户的15个中间版本)
第42行: 第42行:
 
     ...
 
     ...
 
}
 
}
 +
</pre>
 +
==my eg==
 +
<pre>
 +
server  {
 +
listen 80;
 +
listen [::]:80;
 +
#listen 443 ssl http2;
 +
#listen [::]:443 ssl http2;   
 +
 +
    server_name www.cai.net cai.net;
 +
 +
#    return 301 https://www.cai.net$request_uri;
 +
    rewrite ^(.*)$ https://$host$1 permanent;
 +
}
 +
 +
server {
 +
listen 443 ssl http2;
 +
listen [::]:443 ssl http2;   
 +
#    listen 443 ssl;
 +
    server_name www.cai.net cailu.net;
 +
 +
#    if ( $host = 'www.cai.net'  )  {
 +
#        rewrite ^/(.*)$ https://www.cai.net/$1 permanent;
 +
#    }
 +
#这个是不写多了呢
 +
    if ( $host = 'cai.net'  )  {
 +
        rewrite ^/(.*)$ https://www.cai.net/$1 permanent;
 +
    }
 +
 +
</pre>
 +
==不同域名之间跳转 2022==
 +
<pre>
 +
 +
 +
 +
server {
 +
    listen      80;
 +
    server_name kkb.old.com;
 +
 +
    rewrite ^(.*)$ https://kkb.new.com$1 permanent;
 +
 +
今天的情况是 他们没上新代码  我浪费了 半天时间
 +
 +
curl  -I zy.com
 +
HTTP/1.1 301 Moved Permanently
 +
Server: nginx/1.20.2
 +
Date: Mon, 04 Jul 2022 07:30:43 GMT
 +
Content-Type: text/html
 +
Content-Length: 169
 +
Connection: keep-alive
 +
Location: https://qm.com/
 +
 +
 +
 +
 +
 +
 +
server
 +
{
 +
    listen 80;
 +
    listen 443 ssl;
 +
    server_name              m.cailuw.com;
 +
尽量加在 证书后面 尽量前
 +
    return                    301 https://m.cailuw.net$request_uri;
 +
}
 +
</pre>
 +
 +
==host.com跳https:==
 +
<pre>
 +
server {
 +
  listen 80;
 +
  server_name xnathan.com www.xnathan.com;
 +
  return 301 https://xnathan.com$request_uri;
 +
}
 +
</pre>
 +
 +
==www 跳转非 www==
 +
<pre>
 +
if ($host = 'www.xxx.com' ) {
 +
    rewrite ^/(.*)$ http://xxx.com/$1 permanent;
 +
        }
 +
 +
</pre>
 +
 +
=rewrite方法good june 2022=
 +
<pre>
 +
server {
 +
listen 192.168.1.111:80;
 +
server_name test.com;
 +
 +
rewrite ^(.*)$ https://$host$1 permanent;
 +
}
 +
 +
重定向https的另起一个文件
 +
cat traits.d/enable-redirect-https.conf
 +
    if ($scheme !~ 'https') {
 +
        rewrite ^(/.*)$ https://$host$1 permanent;
 +
    }
 +
 +
 +
 +
域名配置的文件记得 include
 +
    include traits.d/enable-php.conf;
 +
    include traits.d/enable-linux.conf;
 +
    include traits.d/enable-redirect-https.conf;
 +
}
 +
  
 
</pre>
 
</pre>
第48行: 第155行:
  
 
[https://blog.hcl.moe/archives/729  Nginx下HTTP强制重定向至HTTPS]
 
[https://blog.hcl.moe/archives/729  Nginx下HTTP强制重定向至HTTPS]
 +
 +
[https://blog.csdn.net/benpaodelulu_guajian/article/details/78456971  服务器 nginx配置ssl并http重定向到https]
 +
 +
[https://blog.csdn.net/wzy_1988/article/details/8549290  nginx强制使用https访问(http跳转到https)]

2022年7月4日 (一) 08:10的最新版本


301强制重定向


#注意  是加在 ssl cer 后面  ,加在 前面 可能当作 是 80的 不生效呢 
#4 blog 
    server {
        listen       80;
       # listen [::]:80 ipv6only=on default_server;
        listen 443 default ssl;
       #listen [::]:80;
       #ssl on;
        ssl_certificate /etc/letsencrypt/live/linuxchina.net/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/linuxchina.net/privkey.pem;

       server_name  linuxchina.net www.linuxchina.net;

 #by evan
    if ($scheme = http) {
           return 301 https://$server_name$request_uri;
    } 
 #by evan
    #    server_name  blog.linuxchina.net;
        charset utf-8;
          root /data/www/evan;
        index index.php index.html index.htm;



#简约格式 

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

server {
    listen 443;
    ...
}

my eg

server  {
	listen 80;
	listen [::]:80;
	#listen 443 ssl http2;
	#listen [::]:443 ssl http2;    
 
    server_name www.cai.net cai.net;

#     return 301 https://www.cai.net$request_uri;
    rewrite ^(.*)$ https://$host$1 permanent;
}

server {
	listen 443 ssl http2;
	listen [::]:443 ssl http2;    
#    listen 443 ssl;
    server_name www.cai.net cailu.net;

#    if ( $host = 'www.cai.net'  )  {
#        rewrite ^/(.*)$ https://www.cai.net/$1 permanent;
#    }
#这个是不写多了呢 
    if ( $host = 'cai.net'  )  {
        rewrite ^/(.*)$ https://www.cai.net/$1 permanent;
    }

不同域名之间跳转 2022




server {
    listen      80;
    server_name kkb.old.com;

    rewrite ^(.*)$ https://kkb.new.com$1 permanent;

今天的情况是 他们没上新代码  我浪费了 半天时间 

 curl   -I zy.com 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.20.2
Date: Mon, 04 Jul 2022 07:30:43 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://qm.com/






server
{
    listen 80;
    listen 443 ssl;
    server_name               m.cailuw.com;
尽量加在 证书后面 尽量前
    return                    301 https://m.cailuw.net$request_uri;
}

host.com跳https:

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

www 跳转非 www

if ($host = 'www.xxx.com' ) {
	    rewrite ^/(.*)$ http://xxx.com/$1 permanent;
        }

rewrite方法good june 2022

server {
	listen	192.168.1.111:80;
	server_name	test.com;
	
	rewrite ^(.*)$	https://$host$1	permanent;
}

 重定向https的另起一个文件 
 cat traits.d/enable-redirect-https.conf 
    if ($scheme !~ 'https') {
        rewrite ^(/.*)$ https://$host$1 permanent;
    }



域名配置的文件记得 include 
    include traits.d/enable-php.conf;
    include traits.d/enable-linux.conf;
    include traits.d/enable-redirect-https.conf;
}


see also

Nginx下HTTP强制重定向至HTTPS

服务器 nginx配置ssl并http重定向到https

nginx强制使用https访问(http跳转到https)