首页 > Web开发 > 详细

【转载】Nginx 配置 http2https

时间:2020-04-08 00:07:31      阅读:70      评论:0      收藏:0      [点我收藏+]

版本一

原文地址

我的 Nginx 配置,域名不带 www 跳转到www,http 强制跳转 https,这些都有了

nginx.conf


server {
	listen 80;
	server_name www.your-domain.com;
	rewrite ^(.*)$  https://www.your-domain.com$1 permanent;
}
server {
	listen 443 ssl;
	server_name www.your-domain.com;

	ssl_certificate /home/ssl_certificate/your-domain.com.pem;
	ssl_certificate_key /home/ssl_certificate/your-domain.com.key; 
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
	ssl_session_timeout  5m;

	if ( $host != ‘www.your-domain.com‘ ) {
		rewrite ^(.*)$ https://www.your-domain.com$1 permanent;
	}
	
	root /var/www/html;

	index index.html index.htm index.nginx-debian.html;

	# server_name _;

	location / {
		proxy_pass http://127.0.0.1:8080;
	}
}

https配置

server {
	# 我们都知道(我们都应该知道),443是 https 的默认端口
	listen 443 ssl;
	server_name www.your-domain.com;
	# 你要有证书,才能 https,免费申请一个吧,七牛云,阿里云都有免费一年的证书
	ssl_certificate /home/ssl_certificate/your-domain.com.pem;
	ssl_certificate_key /home/ssl_certificate/your-domain.com.key; 
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
	ssl_session_timeout  5m;
	# 下面这句就是当识别到 HOST 不是带 www 的全部都 301 带上 www
	if ( $host != ‘www.your-domain.com‘ ) {
		rewrite ^(.*)$ https://www.your-domain.com$1 permanent;
	}
	
	root /var/www/html;

	index index.html index.htm index.nginx-debian.html;

	# server_name _;

	location / {
		# 我是 java web 所以用了 Tomcat ,但是我要用 nginx 做转发,因此有了如下的配置
		proxy_pass http://127.0.0.1:8080;
	}
}

http to https

将 80 与 443 端口分别配置一个 server,让80 端口访问的强制 301 跳转到 https。如下所示:

rewrite ^(.*)$  https://www.your-domain.com$1 permanent;

强制添加 www

nginx 的配置文件可以写这种判断和表达式,总之是很厉害的,仔细观察下面的 if 判断很容易明白讲的什么意思,当 HOST 不是带 www 的访问时 302 到 www 上面。


        # 下面这句就是当识别到 HOST 不是带 www 的全部都 302 带上 www
	if ( $host != ‘www.your-domain.com‘ ) {
		rewrite ^(.*)$ https://www.your-domain.com$1 permanent;
	}

需要注意的是:

if ( $host != ‘www.your-domain.com‘ ) { 

这一句一定要按照格式书写,括号前后的空格必须带着,还有if之后的空格也一样。
如果不!会报错:

unknown directive "if($host!="

评论补充


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

server {
    listen 443 ssl http2;
    server_name www.hichinese.world hichinese.world;
    access_log /app/log/nginx/hichinese_upstream_access.log main;
    error_log /app/log/nginx/hichinese_upstream_error.log;

    if ( $host != ‘www.hichinese.world‘ ) {
        rewrite ^(.*)$ https://www.hichinese.world$1 permanent;
    }
}

版本二

原文地址

Nginx Https配置不带www跳转www


server {
    listen       80;
    server_name morethink.cn,www.morethink.cn;
    return 301 https://www.morethink.cn$request_uri;
}
server {
    listen 443;
    server_name morethink.cn;
    return 301 https://www.morethink.cn$request_uri;
}
server {
    listen 443 default_server ssl;
    server_name  www.morethink.cn;
    ssl on;
    ssl_certificate 1_www.morethink.cn_bundle.crt;
    ssl_certificate_key 2_www.morethink.cn.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    root         /var/www/hexo;
    include /etc/nginx/default.d/*.conf;
    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

【转载】Nginx 配置 http2https

原文:https://www.cnblogs.com/notfound/p/12656953.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!