Sunday, December 22, 2024

Nginx配置Http跳转到Https

Nginx配置Http跳转到Https,需要修改Nginx.conf配置文件:

server {
listen       443;
server_name  www.qiaodahai.com;

ssl                  on;
ssl_certificate      ca.pem;
ssl_certificate_key  ca.key;

ssl_session_timeout  5m;

ssl_protocols  SSLv2 SSLv3 TLSv1;
ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers   on;
error_page 497 "https://$host$uri?$args"; #这是跳转Http请求到Https

location / {
root   html;
index  index.html index.htm;
}
}
server {
listen 80;
server_name www.qiaodahai.com;
rewrite ^/(.*) https://$server_name$1 permanent;    #跳转到Https
}

也就是再添加一个虚拟机server,80端口一个,443端口一个。

但是有些程序只会给你往端口上转发,不会自动修正http为https,这样的程序还不少,例如phpmyadmin:

遇到这样的程序我们需要修改Nginx.conf配置文件,在443的server的fastcgi字段中添加一个语句:

fastcgi_param HTTPS on; #attention!#

例如

location ~ .*.(php|php5)?$
            {
                try_files $uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                fastcgi_param HTTPS on; #attention!#
                include fcgi.conf;
            }

这样就可以了。

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.