Nginx配置https

1.默认情况下ssl模块并未被安装,需要在编译nginx的时候指定–with-http_ssl_module参数。
[root@zabbix vhosts]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

生成证书流程:

 

可以通过以下步骤生成一个简单的证书:
首先,进入你想创建证书和私钥的目录,例如:

cd /usr/local/nginx/conf
创建服务器私钥,命令会让你输入一个口令:

openssl genrsa -des3 -out server.key 1024
创建签名请求的证书(CSR):

openssl req -new -key server.key -out server.csr
在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

配置nginx(我这里是测试机,域名随便)可以达到https和http共存的需求。
http://zabbix.com
https://zabbix.com

server {
      listen 80;
      listen 443 ssl; # ssl写在443端口后面, 这样http和https的链接都可以用
      server_name zabbix.com;

      root /usr/local/nginx/html/zabbix;
      index index.php index.html index.htm;

#       ssl on; #把ssl on;这行注释掉
      ssl_certificate /usr/local/nginx/conf/vhosts/server.crt;
      ssl_certificate_key /usr/local/nginx/conf/vhosts/server.key;
      ....................................................

      }

 

关键在于

上面的listen 80;
listen 443 ssl; 开启80端口

当然,这样玩就没有啥意义了,既然是https,就完全没必要http传输数据啦.我们必须把所有http请求转发到https,

把http重定向到https使用了nginx的重定向命令。也可以使用location中的 proxy_pass http://192.168.0.162:443;
也就是再添加一个虚拟主机,之前网站使用默认80端口
server {
    listen 80;
    server_name zabbix.com;
    rewrite ^/(.*)$ https://zabbix.com/$1 permanent; ####请求zabbix.com:80端口的时候重定向到https://zabbix.com

    root /usr/local/nginx/html/zabbix;
    index dashboard.php index.php index.html index.htm; ###需要首页锁定的比如dashboard.php可写到index.php中
    .....
    ......
  } ####默认使用虚拟主机。
然后添加一个443端口的虚拟主机。
server {
      listen 443;
      server_name zabbix.com;
      root /usr/local/nginx/html/zabbix;
      index dashboard.php index.php index.html index.htm;

      ssl on;

      ssl_certificate vhosts/server.crt; ###需要开启ssl,设置证书的路径。

      ssl_certificate_key vhosts/server.key;

      ........
      ...........
    }

 

另外一种方式:使用外部反向代理到本机的80端口,意思是前端点击之后跳转到https的url,然后反向代理到本机的80端口,这样也可以实现https访问。实现如下:

upstream tw_league {

        server   127.0.0.1:80;

}

 

server {

        listen  443;

        server_name  league.myth.game168.com.tw;

        ssl               on;

        ssl_certificate         vhosts/key/myth.game168.com.tw.crt;    ###需要开启ssl,设置证书的路径。

        ssl_certificate_key     vhosts/key/myth.game168.com.tw.key; ####通过前端的https://xxx:443端口跳转到本机之后,交给upstream模块跳转到本机的80端口,实现https。

        charset utf-8;

        access_log  logs/ssl-tw.sh.9wee.com_access.log main;

        #access_log  off;

 

        location / {

                proxy_pass http://tw_league;

                proxy_redirect off;

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                client_max_body_size 50m;

                client_body_buffer_size 256k;

                proxy_connect_timeout 30;

                proxy_send_timeout 30;

                proxy_read_timeout 60;

                proxy_buffer_size 8k;

                proxy_buffers 8 32k;

                proxy_busy_buffers_size 64k;

                proxy_temp_file_write_size 64k;

                proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

                proxy_max_temp_file_size 128m;

         }

}

posted @ 2017-07-30 16:36  GeminiMp  阅读(452)  评论(0编辑  收藏  举报