nginx配置https

2018-12-05  本文已影响0人  胡乱唱歌ing

概述:nginx配置https,http共存的步骤大概如下
1.系统需要安装openssl
2.nginx开启ssl
3.生成证书密钥文件
4.nginx配置https ,http

1.安装openssl (已经安装了请忽略)

wget http://www.openssl.org/source/openssl-1.0.2f.tar.gz
tar -xzf openssl-1.0.2f.tar.gz
cd openssl-1.0.2f
./config --prefix=/usr/local/openssl
make && make install

检查是否安装成功

which openssl

2.检测nginx 是否安装了with-http_stub_status_module,with-http_ssl_module

/usr/bin/nginx -V  #具体的nginx安装路径根据你的环境而定
image.png

3.安装nginx SSL模块 需要从新编译nginx

cd /usr/local/src/nginx-1.12.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make

4.生成证书密钥文件(存放目录nginx/conf/ssl)

4.1 创建服务器证书密钥文件 server.key

mkdir  /usr/local/nginx/conf/ssl
cd /usr/local/nginx/conf/ssl
openssl genrsa -des3 -out server.key 2048

4.2 创建服务器证书的申请文件 server.csr

openssl req -new -key server.key -out server.csr
输出内容为:
Enter pass phrase for root.key: ← 输入前面创建的密码 
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音 
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不输入 
Common Name (eg, YOUR name) []: ← 此时不输入 
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不输入 
An optional company name []: ← 可以不输入

4.3 备份一份服务器密钥文件

cp server.key server.key.org

4.4 去除文件口令

openssl rsa -in server.key.org -out server.key

4.5 生成证书文件server.crt

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
image.png

5. 配置nginx 虚拟目录

https.proxy.test.com.conf

server
{
    listen 443 default ssl;
    #listen [::]:80 default_server ipv6only=on;
    ssl on;
    ##证书(公钥.发送到客户端的)
    ssl_certificate ssl/server.crt;
    #私钥,
    ssl_certificate_key ssl/server.key;
    ssl_protocols SSLv2 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;

    server_name proxy.test.com;
    index index.html index.htm index.php;
    root  /home/wwwroot/proxy.test.com;

    include enable-php.conf;

    location /nginx_status
    {
        stub_status on;
        access_log   off;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ /\.
    {
        deny all;
    }
}

6 .配置http重定向到https

http.proxy.test.com.conf

server
{
    listen 80 ;
    server_name proxy.test.com;
    #index index.html index.htm index.php;
    #root  /home/wwwroot/proxy.test.com;

    #error_page   404   /404.html;
    include enable-php.conf;

    location /nginx_status
    {
        stub_status on;
        access_log   off;
    }
  
    #重定向到https
    if ($scheme = 'http') {
      rewrite ^(.*)$ https://$host$uri;
    }  
   #或
   # return 301 https://$server_name$request_uri; 
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ /\.
    {
        deny all;
    }
 
}

打开浏览器 输入http.proxy.test.com
打开浏览器 输入https.proxy.test.com
如果两个地址都指向同一个地方就OK啦

上一篇 下一篇

猜你喜欢

热点阅读