nginx 80转443 http自动跳转到https

首先贴出nginx.conf:

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    client_max_body_size 10m;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
	server {
        listen       80;
		server_name 168.dm168.cn;	
		rewrite ^ https://$http_host$request_uri? permanent;   	
    }

    # HTTPS server
	server {
		listen 443 ssl;
		server_name www.mccken.com;
		root html;
		index index.html index.htm;
		ssl_certificate     /opt/ssl/ssl.pem;
		ssl_certificate_key /opt/ssl/ssl.key;
		ssl_session_timeout 5m;
		ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
		ssl_prefer_server_ciphers on;
		location / {
			root   html;
			index  index.html index.htm;
			proxy_set_header Host $host;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://【ip】:8080;
		}
	}
}

nginx 80转443 http自动跳转到https_第1张图片
说明:

  1. nginx的版本是 nginx-1.15.6,在 配置文件中不需要 ssl on;
  2. 下面两个是ssl证书的存放地址,如果存放的证书与nginx.con 中server_name申请的域名不一致,就会出现“安全证书有问题”这种情况
ssl_certificate     /opt/ssl/ssl.pem;
ssl_certificate_key /opt/ssl/ssl.key;

nginx 80转443 http自动跳转到https_第2张图片

  1. rewrite ^ https://$http_host$request_uri? permanent; 用于实现80端口重定向到443
  2. 使用该配置文件,如果报错如下,则说明没有将ssl模块编译进nginx
    [emerg] 10464#0: unknown directive "ssl" in /usr/local/ nginx-1.15.6/conf/nginx.conf
    可参考文章:Nginx配置SSL报错 nginx: [emerg] unknown directive “ssl”

另外参考文章:
· nginx使用ssl模块配置支持HTTPS访问
· nginx配置文件nginx.conf超详细讲解

你可能感兴趣的:(nginx)