## 反向代理
> 開發時,很多第三方規定只能使用80端口(比如微信開發),而frp代理80端口時會和nginx沖突,這就尷尬了,所以我們要想個辦法來解決此問題:**使用Nginx反向代理配置來 隱藏 8080 端口。**
* * * * *
#### 解決frp和nginx80端口沖突問題:
[如果服務器上80端口被占用的話 只能選擇別的端口嗎? · Issue #124 · fatedier/frp](https://github.com/fatedier/frp/issues/124)
~~~
nginx反向代理是不是要做到IIS和frp前面?即由nginx來監聽80端口,根據域名的不同,分到IIS或者frep。這樣公網服務器既可以自己做網站服務,又可以提供穿透內網發布Web?
這樣原來的IIS主機頭就不用了,而是由ngnix來實現?
嗯嗯,非常對
~~~
[訪問內網web不通過域名 · Issue #75 · fatedier/frp](https://github.com/fatedier/frp/issues/75)
[怎么使用域名80端口訪問? · Issue #883 · fatedier/frp](https://github.com/fatedier/frp/issues/883)
* * * * *
### 解決方案:
1. 增加代理文件:/usr/local/nginx/conf/reverse-proxy/x.xxx.cn.conf;
```
server {
listen 80;
server_name x.xxx.cn;
#標記代理
add_header This-Is-Proxy-Pass "$host => http://nginxServer (frp)";
location / {
proxy_redirect off;
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_pass http://nginxServer;
}
}
```
2. 修改文件:/usr/local/nginx/conf/nginx.conf(如果不用upstream 則這步可省略)
```
http {
……
######################## upstream ############################
upstream nginxServer {
server 114.116.67.137:8080;
}
……
}
```
>[tip] 代理文件不能放在這里!只能放在需要代理的 vhost.conf 中。
3. 在需要代理的 vhost.conf 中引入代理配置文件
如果不想使用代理了,直接注釋掉 `include /usr/local/nginx/conf/reverse-proxy/x.xxx.cn.conf;` 這行即可關閉代理。
>[tip] **注意這個反向代理實在服務器上配置的,是為了解決frp 80端口沖突不能用導致開發時不方便,使用代理以解決此問題,我們將80代理到8080就可以達到效果了(對外開放80,但其實被代理到了8080,然后穿透到本地開發機),本地不需要這樣配置哦。**
>[tip] 配置好代理后如不生效可能需要 `service nginx restart` 一下,重啟而不是 `reload`
* * * * *
### 擴展
[Frp 隱藏 8080 端口 – 碼農的世界](https://free-e.net/165)
[Linux運維學習筆記之三十:Nginx反向代理 - CSDN博客](https://blog.csdn.net/rumengjian/article/details/80451711)
>[tip] upstream模塊應放于nginx.conf配置的http{}標簽內。
[搭建nginx反向代理用做內網域名轉發 – 運維生存時間](http://www.ttlsa.com/nginx/use-nginx-proxy/)
~~~
client_max_body_size 50m; #緩沖區代理緩沖用戶端請求的最大字節數,可以理解為保存到本地再傳給用戶
client_body_buffer_size 256k;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
proxy_connect_timeout 300s; #nginx跟后端服務器連接超時時間(代理連接超時)
proxy_read_timeout 300s; #連接成功后,后端服務器響應時間(代理接收超時)
proxy_send_timeout 300s;
proxy_buffer_size 64k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
proxy_buffers 4 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置
proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大于這個值,將從upstream服務器傳遞請求,而不緩沖到磁盤
proxy_ignore_client_abort on; #不允許代理端主動關閉連接
~~~
[Nginx反向代理以及負載均衡配置 - 程序媛_Mickey - 博客園](https://www.cnblogs.com/Miss-mickey/p/6734831.html)
[使用Nginx實現反向代理 - CSDN博客](https://blog.csdn.net/qw_xingzhe/article/details/79580800)
[還是少不了虛擬機 · php筆記 · 看云](http://www.hmoore.net/xiak/php-node/558397)
* * * * *
### https時的代理
將80的https代理到8080中去。
/usr/local/nginx/conf/reverse-proxy/x.xxx.cn.conf;
```
server {
listen 80;
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/conf/ssl/x.xxx.cn.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/x.xxx.cn.key;
server_name x.xxx.cn;
#標記代理
add_header This-Is-Proxy-Pass "$host => https://x.xxx.cn:8080 (frp)";
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_ssl_server_name on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
#看來這里要寫死域名才行啊
proxy_pass https://x.xxx.cn:8080;
}
}
```
配置正常,可總是報錯:`[W] [vhost.go:136] get hostname from http/https request error: Unknow error`,80端口的https請求代理不過來啊。
> 雖然還沒有找到真正的問題,但目前這也算是臨時解決了這個問題。
> 猜想是因為https是加密的,直接轉發到ip:pro是不能識別的,所以報無法找到host,只能完整的寫要轉發的域名和端口才可以。
[NGINX HTTPS and FRPS · Issue #610 · fatedier/frp](https://github.com/fatedier/frp/issues/610)
[nginx https轉發frps · Issue #671 · fatedier/frp](https://github.com/fatedier/frp/issues/671)
[nginx的反向代理模塊 參數proxy_pass,proxy_method,proxy_hide_ - adbug的個人空間 - 開源中國](https://my.oschina.net/u/1038053/blog/619993)
[nginx之proxy_pass代理后端https請求完全拆解 - 永福的博客 - 開源中國](https://my.oschina.net/foreverich/blog/1517128?utm_medium=referral)
[請教一個 nginx 反向代理 https 的問題? - V2EX](https://www.v2ex.com/t/253715)
[有人配置成功過 nginx 反代 frp 的 https 嘛? - V2EX](https://www.v2ex.com/t/378393)
[nginx模塊之ngx_http_proxy_module - CSDN博客](https://blog.csdn.net/yum_root/article/details/77509156)
[Nginx反向代理http和https - CSDN博客](https://blog.csdn.net/qq_32642039/article/details/78696119)
看來對于https的反向代理可不像http反向代理那么簡單啊!
* * * * *
### 補充
注意到默認有這個代理配置文件:/usr/local/nginx/conf/proxy.conf
```
proxy_connect_timeout 300s;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
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 X-Forwarded-Proto $scheme;
```
* * * * *
last update:2018-8-8 21:40:30