您的当前位置:首页正文

Nginx配置ssl

2024-11-29 来源:个人技术集锦

Nginx配置ssl

nginx配置ssl实现https加密,主要是配置nginx.conf

1.编辑nginx.conf

vi /usr/local/nginx/sbin/nginx.conf

2.找到你要配置的server,按以下结构编辑(http会强制转换https)。

server {
		listen       80;
        listen       443 ssl;
        server_name  www.demo.com demo.com;

        ssl_certificate      /usr/local/ssl/demo.crt;
        ssl_certificate_key  /usr/local/ssl/demo.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
		

        if ($server_port = 80 ) {
                return 301 https://$host$request_uri;
        }
        proxy_set_header Host $host;
      	proxy_set_header X-Real-IP $remote_addr;
      	proxy_set_header REMOTE-HOST $remote_addr;
      	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        
        
        location / {
            proxy_pass http://127.0.0.1:8080;
            
			proxy_set_header Host $host;
        	proxy_set_header X-Real-IP $remote_addr;
        	proxy_set_header REMOTE-HOST $remote_addr;
        	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
		#让http请求重定向到https请求
        error_page 497  https://$host$request_uri;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
显示全文