首页 > 运营 > 帮助中心 > 正文

Lnmp反向代理设置(解决80端口被屏蔽的问题)

2020-01-29 19:35:23
字体:
来源:转载
供稿:网友

Lnmp 一键安装包 简单反向代理设置

Nginx是一个高性能的HTTP和反向代理服务器,使用Nginx,只需要简单的几条命令保存到文件,即可实现简单、基本反向代理功能。

这里以LNMP一键安装包为例,首先请创建虚拟主机。创建虚拟主机请参考:

https://lnmp.org/faq/lnmp-vhost-add-howto.html

 

一、进入相关目录

如创建一个网址为“www.test1.com”的虚拟主机

进入LNMP一键包的虚拟主机配置文件夹,执行:cd /usr/local/nginx/conf/vhost

找到刚刚创建的“www.test1.com.conf”文件,并编辑。

 

二、添加反向代理规则

1、方法一

server

{

listen 80;

server_name www.test1.com;

 

location / {

proxy_set_header  $host  www.test2.com; 

 

proxy_pass http://www.test2.com/;

proxy_redirect off;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

 

以上代码,表示使用“www.test1.com”反向代理“www.test2.com”,然后保存文件即可。

 

备注:用这种反向代理方法也可以解决国内ip被封80端口的问题。只需要给被反向代理的域名加上端口,例如上面的可以改成:http://www.test2.com:82/;

这样当访问www.test1.com时,就可以访问www.test2.com:82的网站了。

注意:如果没有上面红色的那段代码,会导致访问www.test1.com静态页时正常,但访问动态页时,会跳转到www.test2.com 。这点很重要。 

 

2、方法二

上面的方法就可以实现反向代理,这里再提供一种代码的写法:

 

server {

listen 80;

server_name     www.test1.com;

proxy_set_header REMOTE-HOST $remote_addr;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

access_log /home/www/logs/www.test1.com_access.log;

 

location /robots.txt {

root /home/www/www.test1.com;

}

 

location / {

proxy_pass http://www.test2.com;

}  

} 

 

三、测试 

1、测试规则是否正确

/usr/local/nginx/sbin/nginx -t

若提示:“the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx.conf test is successful”表示正常,若有错误请根据错误提示排除问题。

 

2、重载nginx规则 

service nginx reload

执行以上代码即可生效。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表