Nginx + Frp + Let'sEncrypt 泛域名證書


幾個日夜,無數坑。

背景是:

  • 目前有備案的域名只有一個,而這個已經在生產環境采用。所以如果要采用這個域名,將不得不用三級泛域名證書,形如*.dev.aaa.com。
  • 由於首要目的是開發用(微信小程序),后台語言是asp.net core,出於開發方便考慮,Web服務器用Kestrel。(當然,最終解決方案於此無關)
  • 阿里雲服務器,CentOS 7.4,域名也在阿里雲。

 

一開始用ngrok但始終不能成功,后改用Frp,確實比ngrok好很多,但依然無法配置成功https訪問。

最終,采用Nginx + Frp的方案,網站無需實現https,SSL在Nginx處用反向代理實現。

 

GO!

 

一、域名解析:

 

 

二、安裝Frp:

參考:

Frps一鍵安裝腳本,帶Frpc Windows便捷啟動腳本

 

來自 <https://www.moerats.com/archives/797/>

 

三、配置Frp:

(1) 服務器端:

用f.sh命令修改,或直接改文件:

# vim /usr/local/frps/frps.ini

主要要改:

vhost_http_port = 8090 #避開nginx要用的80

vhost_https_port = 8443 #其實沒什么用,因為靠Nginx來處理https

dashboard_user = admin

dashboard_pwd = admin

token = IamPassword

subdomain_host = dev.aaa.com

如果是直接改的ini文件,保存后別忘了用systemctl restart frps重啟服務。

(2)安全組或防火牆配置

Frps.ini文件中配置的端口確保在阿里雲的安全組中打開。當然,還包括默認的7000,7500等端口。如果不是雲主機,那就是防火牆同樣操作。

(3) 客戶端

Frpc.ini文件:

[common]

server_addr = 服務器IP

token = IamPassword

server_port = 7000

# protocol = kcp   這個不知道為什么,一開就連不上

log_file = ./frpc.log

log_level = info

log_max_days = 3

 

[testdevhttp]  #注意,這個名稱不能重

type = http

local_port = 5000

subdomain = test  

# http_user = admin

# http_pwd = admin

……

 

同樣,5000端口要打開妨火牆。

這個時候內網網站就應該監聽5000端口,確保http://localhost:5000能打開網站。

執行start.bat打開客戶端。

(4)驗證:

訪問http://服務器IP:7500,能進入管理界面(需要輸入dashboard_user和pwd);

訪問http://test.dev.aaa.com:8090 能打開網站

 

四、安裝nginx

sudo yum install nginx

vim /etc/nginx/nginx.conf

把server_name _;改成:server_name *.dev.aaa.com;

保存退出。

 

用sudo nginx -t驗證修改正確,然后用

sudo systemctl reload nginx 重載nginx。

 

確保安全組或防火牆配置的80和443端口是打開的。

 

此時打開http://*.dev.aaa.com, 應能看到nginx的默認站點。 *可以是任意字符組合。

 

五、安裝Let's Encrypt

參考:

Let's Encrypt 安裝配置教程,免費的 SSL 證書

來自 <https://segmentfault.com/a/1190000017194280>

 

但要注意,把其中:

./certbot-auto certonly  -d *.you.cn --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory

替換成:

./certbot-auto --nginx -d *.dev.aaa.com  --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory

(是不是certonly要去掉忘了——應該是要去掉,關鍵是要加上--nginx,去掉--manual)

執行后會有一系列問題,回答即可。關於nginx的,會詢問是否全部自動redirect http 為 https,之后會對nginx.conf文件做一系列修改等等。

其中和nginx無關的一些內容可以參考上文。

 

六、設置反向代理

Vim nginx.conf

看到nginx.conf文件已經發生了很大變化,找到:

    server {

        server_name  *.dev.aaa.com;

        root         /usr/share/nginx/html;

 

        # Load configuration files for the default server block.

        include /etc/nginx/default.d/*.conf;

 

        location / {

#手動添加下面內容:

                proxy_pass http://127.0.0.1:8090;

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_set_header REMOTE-HOST $remote_addr;

                add_header X-Cache $upstream_cache_status;expires 12h;

    #開啟wss(參看https://gblog.sherlocky.com/websocket-jie-he-nginx-shi-xian-yu-ming-ji-wss-xie-yi-fang-wen/):

 

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

 

        }

 

        error_page 404 /404.html;

            location = /40x.html {

        }

 

        error_page 500 502 503 504 /50x.html;

            location = /50x.html {

        }

 

    listen [::]:443 ssl ipv6only=on; # managed by Certbot

    listen 443 ssl; # managed by Certbot

    ssl_certificate /etc/letsencrypt/live/dev.aaa.com/fullchain.pem; # managed by Certbot

    ssl_certificate_key /etc/letsencrypt/live/dev.aaa.com/privkey.pem; # managed by Certbot

    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

 

}

保存退出。

然后再繼續

sudo nginx -t

sudo systemctl reload nginx

 

順利的話,此時再訪問站點,就應該能看到帥氣的小鎖。

 

 

 

 

七、續簽與其他

參看:更新證書:

 

來自 <https://www.cnblogs.com/peteremperor/p/9994713.html>

 

更好的安全性方面,參看:

Step 5 — Updating Diffie-Hellman Parameters

 

來自 <https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-centos-7>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM