nginx https 配置

4 篇文章 0 订阅
4 篇文章 0 订阅

2017年1月1日起,苹果强制所有APP的请求都得是https的协议,没办法,只有赶紧将http改成https了,下面记录下配置过程供大家借鉴,同时也方便自己下次再配置

1、首先必须确认你的服务器开启了openssl模块   查看方法:nginx -V ,如出现 --with-http-ssl-module字样,则说明已开启

nginx version: nginx/1.10.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-ld-opt=' -Wl,-E'

2、进入nginx目录,进行私钥配置

      cd /etc/nginx    mkdir key    cd key

3、openssl genrsa -des3 -out server.key 1024(这步设置证书密码)

4、签发证书(这步需要设置一些相关公司信息)

     openssl req -new -key server.key -out server.csr

以下部分从微信支付平台之https搭建中拷过来的

Country Name: CN                                                                                                         //您所在国家的ISO标准代号,中国为CN

State or Province Name:guandong                                                                               //您单位所在地省/自治区/直辖市

Locality Name:shenzhen                                                                                             //您单位所在地的市/县/区

Organization Name: Tencent Technology (Shenzhen) Company Limited                 //您单位/机构/企业合法的名称

Organizational Unit Name: R&D                                                                               //部门名称

Common Name: www.example.com                                                                      //通用名,例如:www.itrus.com.cn。此项必须与您访问提供SSL服务的服务器时所应用的域名完全匹配。

Email Address:                                                                                                       //您的邮件地址,不必输入,直接回车跳过

"extra"attributes                                                                                                        //以下信息不必输入,回车跳过直到命令执行完毕。

5、删除服务器私钥口令

     cp server.key server.key.ori

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

6、生成使用签名请求证书与私钥生成自签证书

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

7、开始修改.conf配置文件

    

server {
        #listen   80;
        listen 443;
        ssl on;
        ssl_certificate key/server.crt;
        ssl_certificate_key key/server.key;
    
        server_name 你的域名;

        location / {
            index  index.htm index.html index.php;

            try_files  $uri  /index.php$uri;
        }

        location ~ .+\.php($|/) {
            root        系统根目录;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;


            fastcgi_split_path_info  ^(.+\.php)(/.*)$;
            fastcgi_param  PATH_INFO $fastcgi_path_info;


            include        fastcgi.conf;
        }
    }

8、查看443端口是否生效
    [root@VM_213_59_centos conf.d]# netstat -lntup|grep 443
    tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      4320/nginx

8、重启nginx    service nginx restart

9、测试,浏览器中输入IP进行访问(此处可能会提示不安全的链接,没关系,因为我们的密钥并不是第三方权威机构发的,是我们自己生成的)


严重申明:在做第三方支付的时候,常用的当然就微信和支付宝了。当我们改成https后,微信的各种接口还是正常调用,不会受影响的,但是支付宝的回调通知地址这里会有影响 ,如果你的https不是第三方认证过的(即浏览器打开显示不是绿色),那么支付宝在调用回调通知地址时会失败,所以我们就得将服务配置成http,https共存的方式,下面贴出配置代码:

server {
	//主要区别在这里
        listen   80 default backlog=2048;
        listen 443 ssl;
        #ssl on;
        ssl_certificate key/server.crt;
        ssl_certificate_key key/server.key;

      
        server_name 域名;

        location / {
            index  index.htm index.html index.php;

            try_files  $uri  /index.php$uri;
        }

        location ~ .+\.php($|/) {
            root        系统根目录;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;


            fastcgi_split_path_info  ^(.+\.php)(/.*)$;
            fastcgi_param  PATH_INFO $fastcgi_path_info;


            include        fastcgi.conf;
        }
    } 





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值