Centos Nginx搭建https

場景:開發微信小程序必須https接口,並且url中不含端口號。所以考慮用centos+nginx反向代理實現


1.安裝ngnx

#yum install openssl
#yum install openssl-devel

2下載安裝Nginx

#cd /home
#mkdir nginx-src
#cd nginx-src
#wget http://nginx.org/download/nginx-1.11.8.tar.gz
#tar -xzf nginx-1.11.8.tar.gz
#cd nginx-1.11.8

#./configure --prefix=/usr/local --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

--prefix指定目錄安裝,不帶則默認安裝到/usr/local

一定要帶上 --with-http_ssl_module ,不然後面啓動的時候報[emerg] 10464#0: unknown directive “ssl” in /usr/local/nginx-0.6.32/conf/nginx.conf:74”

#make

#make install


3.配置證書

#make /usr/local/nginx

#mkdir cert

本人用的是阿里雲(www.aliyun.com)的免費證書,只要有備過案的域名可申請。網上也有其他免費證書,或者通過jdk生成。

拷貝阿里雲提供的213998735230175.key、213998735230175.pem至cert目錄

#mkdir cert

添加如下內容至nginx.conf

 server {
    listen 443;
    server_name wxapp.yiqiweiquan.cn;
    ssl on;
    root html;
    index index.html index.htm;
    ssl_certificate  /usr/local/nginx/cert/213998735230175.pem;
    ssl_certificate_key  /usr/local/nginx/cert/213998735230175.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        root html;
        index index.html index.htm;
        if ($host ~ ^(wxapp)\.yiqiweiquan\.com$){
               #rewrite ^/$ /api/$request_uri last;
               proxy_pass http://0.0.0.0:8080;
            }
    }
}

#cd /usr/local/nginx/sbin/

#cd /usr/local/nginx/sbin/

./nginx

報錯:nginx: [emerg] getpwnam("www") failed

#cd /usr/local/nginx/sbin

#groupadd -f www

#useradd -g www www

#./nginx


訪問:https://wxapp.yiqiweiquan.cn





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