centos7.x下環境搭建(五)—nginx搭建https服務

https證書獲取

十大免費SSL證書
https://blog.csdn.net/ithomer/article/details/78075006html

若是咱們用的是阿里雲或騰訊雲,他們都提供了免費版的ssl證書,咱們直接下載就能夠了,這裏我使用的是阿里雲提供的免費證書nginx

修改nginx配置

一、在nginx安裝目錄下建立cert目錄並將.pem和.key的證書拷貝到該目錄下
.crt文件:是證書文件,crt是pem文件的擴展名。
.key文件:證書的私鑰文件瀏覽器

二、nginx.conf配置安全

若是咱們須要配置多站點的話,咱們在根目錄下建立一個vhost目錄,並新建.conf結尾的文件,加入如下內容服務器

server {
    listen              443 ssl;
    server_name         test.test.cn;
    ssl_certificate     /etc/nginx/cert/2283621_test.cn.pem;
    ssl_certificate_key /etc/nginx/cert/2283621_test.cn.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
         location / {
           proxy_set_header Host $host;
           proxy_set_header X-Real-Ip $remote_addr;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_pass http://localhost:7001;
        }
 }

而後修改nginx.conf配置文件,並在http節點裏面最後一行添加以下配置tcp

include /etc/nginx/vhost/*.conf;

這樣在配置多站點的時候,咱們就直接能夠在vhost下新建一個.conf結尾的文件就好了阿里雲

三、轉發80端口到https.net

配置完https以後,默認端口是443,但若是使用http請求的話,並不會跳轉到https,這時候咱們須要將80端口轉發到https上面來
添加80端口監聽listen 80日誌

server {
    listen       80;
    listen              443 ssl;
    server_name         love.diankr.cn;
    ssl_certificate     /etc/nginx/cert/2283621_test.cn.pem;
    ssl_certificate_key /etc/nginx/cert/2283621_test.cn.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
         location / {
           proxy_set_header Host $host;
           proxy_set_header X-Real-Ip $remote_addr;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_pass http://localhost:7001;
        }
 }

這樣在訪問http://開頭的地址的時候會自動跳轉到https地址下code

添加iptables規則開放80和443端口

nginx安裝完以後除了須要開放80端口以外,還須要開放443端口

#添加如下iptables規則開放80端口
iptables -I INPUT -p tcp --dport 80 -j ACCEPT

#iptables添加2條規則用於開放443端口
iptables -A FORWARD -p tcp --dport 443 -j ACCEPT
iptables -A FORWARD -p tcp --sport 443 -j ACCEPT
規則1用於放行客戶端請求https服務的報文
規則2用於放行https服務器發送給客戶端的報文

查看nginx錯誤日誌

tail -f -n 20  /var/log/nginx/error.log

問題總結

一、nginx: [warn] the "ssl" directive is deprecated的解決方法

這是一個warn警告,nginx提示ssl這個指令已經不建議使用,要使用listen ... ssl替代。網上查找nginx更新日誌裏面,也有提到:
Change: the 「ssl」 directive is deprecated; the 「ssl」 parameter of the 「listen」 directive should be used instead.
ssl不建議做爲一個指令使用,而是應該listen指令的一個參數。

解決
若是使用listen 443 ssl,刪除ssl on就好了。

二、在配置好ssl以後,以及將443端口添加到了阿里雲的安全組,瀏覽器訪問最終仍是沒法訪問,提示拒絕了請求連接
分析以後發現仍是跟防火牆有關係
解決

#清除系統中防火牆默認規則
iptables -F

參考閱讀

https://www.jianshu.com/p/8ae92227c4fe
https://help.aliyun.com/knowledge_detail/95491.html?spm=5176.2020520163.cas.33.4f7dfDOHfDOHtD

相關文章
相關標籤/搜索