golang學習筆記9 beego nginx 部署 nginx 反向代理 golang web
Nginx 部署 - beego: 簡約 & 強大並存的 Go 應用框架
https://beego.me/docs/deploy/nginx.md
Go 是一個獨立的 HTTP 服務器,但是我們有些時候為了 nginx 可以幫我做很多工作,例如訪問日志,cc 攻擊,靜態服務等,nginx 已經做的很成熟了,Go 只要專注於業務邏輯和功能就好,所以通過 nginx 配置代理就可以實現多應用同時部署,如下就是典型的兩個應用共享 80 端口,通過不同的域名訪問,反向代理到不同的應用。
server { listen 80; server_name .a.com; charset utf-8; access_log /home/a.com.access.log; location /(css|js|fonts|img)/ { access_log off; expires 1d; root "/path/to/app_a/static"; try_files $uri @backend; } location / { try_files /_not_exists_ @backend; } location @backend { proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:8080; } } server { listen 80; server_name .b.com; charset utf-8; access_log /home/b.com.access.log main; location /(css|js|fonts|img)/ { access_log off; expires 1d; root "/path/to/app_b/static"; try_files $uri @backend; } location / { try_files /_not_exists_ @backend; } location @backend { proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:8081; } }
現有兩個Golang Web服務程序: web1(8080), web2(8081), 兩個域名:www.xxxxx.com, www.yyyy.com
在一台機器上,使xxxxx.com 解析到 8080端口,yyyy.com解析到8081端口
使用nginx進行反向代理的方法:
一、在nginx配置文件中,添加如下配置
server{
listen 80;
server_name www.xxxxx.com xxxxx.com;
location /{
try_files /_not_exists @backend;
}
location @backend{
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:8080;
}
}
域名xxxxx.com,www.xxxxx.com即可解析到服務器 8080端口
server{
listen 80;
server_name www.yyyy.com yyyy.com;
location /{
try_files /_not_exists @backend;
}
location @backend{
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:8081;
}
}
域名yyyy.com,www.yyyy.com即可解析到服務器 8081端口。
---------------------------
