nginx配置https

概述: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啦

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 浏览器地址栏标志着 HTTPS 的绿色小锁头从心理层面上可以给用户专业安全的心理暗示,本文简单总结一下如何在 Ng...
    QieZi阅读 38,074评论 0 10
  • 在如今越来越强调隐私与数据安全的情况下,我们的资源访问当然也要尽可能的安全,我们知道 HTTP 协议是明文传输的,...
    雨林_a1d6阅读 1,542评论 0 7
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 配置站点使用 https,并且将 http 重定向至 https。 1. nginx 的 ssl 模块安装 查看 ...
    YuJinpan阅读 1,705评论 0 18
  • 周六,宝贝一大早就起床自己去玩了,昨晚给姥姥打电话哭了,说好久没去姥姥家了,都想姥姥了,约好今天去姥姥玩,我收拾完...
    妮妮哲阅读 145评论 0 2