关于NGINX反向代理使用域名代理问题记录
2018-08-31 本文已影响62人
小钟钟同学
由于涉及资质问题,我们的域名在某些场景下有点问题,所以需要使用到第三方的符合资质要求的域名。
因为在不改动原有的基础上我们需要使用他们的域名进行反向代理到我们这的接口服务上(但不能影响原来我们的已有的接口服务)
故此,我们需要配置一个反向代理的给到有资质的合作方:
需求就是:
原来我们接口和页面的请求是:
http://我们自己已有的线上域名.cn/
变为:
http://合作方分配的有资质的域名.cn/匹配我们合作方的路径/
即访问:
http://xcx.xxxxxxxxx.cn/
或
http://hezuof.xxxxxxxxx.cn/wmxiangmumingcinge/
的效果是一样。
但是配置的时候,我们希望使用的是代理域名进行跳转处理,测试时候发现有两个小问题点:
server {
listen 80;
charset utf-8;
server_name hezuof.XXXXXXXXXXXXX.cn;
location ^~ /wmxiangmumingcinge/ {
proxy_pass http://xcx.xxxxxxxxx.cn/:18120/;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
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_connect_timeout 300; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时)
}
access_log /data/logs/nginx/xcx.xxxxx_cn_html.log main;
}
1:使用域名代理的时候,在最后面记得需要加上''/'',否则会出现404的问题
2:使用域名代理的时候,必须携带上对的端口号,并且不能使用80端口
所以需要修改的地方:

====================================================
2018-11-28 10:12:42更新
=====================================================
关于反向代理:
因为白名单问题,线上的两个IP一个属于白名单的范畴,一个不属于。
我们需要通过已经加白的IP访问对应的服务器。
所以我们的需要使用反向代理的方式使用属于白名单范畴的服务器代理访问。
配置反向代理中遇到的问题点:
proxy_pass http://xxxxx.wo.com.cn;
后面不能带有斜杠,加上斜杠的会nignx 会提示错误信息如下:
[root@web-1 www]# nginx -t
nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /usr/local/nginx/conf/vhost/ceshidaili].conf:21
所有正确的姿势应该是如下完整的配置:
server {
listen 1988;
charset utf-8;
server_name xxx.xxxx.xxx.xxxx;
location /favicon.ico {
log_not_found off;
access_log off;
}
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
index login.html Index.html index.html;
}
location ~* ^/(rest|course|feedback|course_content|activity)/ {
proxy_pass http://xxxxx.wo.com.cn;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
proxy_connect_timeout 300; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时)
}
access_log /data/logs/nginx/ceshidail_html.log main;
}
后续访问对应的地址:

