nginx https 配置

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;
        }
    } 





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章