nginx proxy_pass反向代理配置实例分析
发布时间 - 2023-05-13 00:00:00 点击率:次下面举个小实例说明下:
centos7系统库中默认是没有nginx的rpm包的,所以我们自己需要先更新下rpm依赖库
1)使用yum安装nginx需要包括nginx的库,安装nginx的库
[root@localhost ~]# rpm -uvh http://nginx.org/packages/centos/7/noarch/rpms/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2)使用下面命令安装nginx
[root@localhost ~]# yum install nginx
3)nginx配置
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
}
[root@localhost conf.d]# cat /var/www/html/index.html
this is page of test!!!!4)启动nginx
[root@localhost ~]# service nginx start //或者使用 systemctl start nginx.service
5)测试访问(103.110.186.23是192.168.1.23机器的外网ip)
[root@localhost conf.d]# curl http://192.168.1.23 this is page of test!!!!
看看下面几种情况:分别用http://192.168.1.23/proxy/index.html进行访问测试
为了方便测试,先在另一台机器192.168.1.5上部署一个8090端口的nginx,配置如下:
[root@bastion-idc ~]# cat /usr/local/nginx/conf/vhosts/haha.conf
server {
listen 8090;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
}
[root@bastion-idc ~]# cat /var/www/html/index.html
this is 192.168.1.5
[root@bastion-idc ~]# /usr/local/nginx/sbin/nginx -s reload测试访问(103.110.186.5是192.168.1.5的外网ip):
[root@bastion-idc ~]# curl http://192.168.1.5:8090 this is 192.168.1.5
192.168.1.23作为nginx反向代理机器,nginx配置如下:
1)第一种情况:
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy/ {
proxy_pass http://192.168.1.5:8090/;
}
}这样,访问http://192.168.1.23/proxy/就会被代理到http://192.168.1.5:8090/。p匹配的proxy目录不需要存在根目录/var/www/html里面
注意,终端里如果访问http://192.168.1.23/proxy(即后面不带"/"),则会访问失败!因为proxy_pass配置的url后面加了"/"
[root@localhost conf.d]# curl http://192.168.1.23/proxy/ this is 192.168.1.5 [root@localhost conf.d]# curl http://192.168.1.23/proxy301 moved permanently 301 moved permanently
nginx/1.10.3
页面访问http://103.110.186.23/proxy的时候,会自动加上"/”(同理是由于proxy_pass配置的url后面加了"/"),并反代到http://103.110.186.5:8090的结果
2)第二种情况,proxy_pass配置的url后面不加"/"
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy/ {
proxy_pass http://192.168.1.5:8090;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service那么访问http://192.168.1.23/proxy或http://192.168.1.23/proxy/,都会失败!
这样配置后,访问http://192.168.1.23/proxy/就会被反向代理到http://192.168.1.5:8090/proxy/
3)第三种情况
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy/ {
proxy_pass http://192.168.1.5:8090/haha/;
}
}
[ro
ot@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]# curl http://192.168.1.23/proxy/
192.168.1.5 haha-index.html这样配置的话,访问http://103.110.186.23/proxy代理到http://192.168.1.5:8090/haha/
4)第四种情况:相对于第三种配置的url不加"/"
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy/ {
proxy_pass http://192.168.1.5:8090/haha;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html
192.168.1.5 hahaindex.html上面配置后,访问http://192.168.1.23/proxy/index.html就会被代理到http://192.168.1.5:8090/hahaindex.html
同理,访问http://192.168.1.23/proxy/test.html就会被代理到http://192.168.1.5:8090/hahatest.html
[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html 192.168.1.5 hahaindex.html
注意,这种情况下,不能直接访问http://192.168.1.23/proxy/,后面就算是默认的index.html文件也要跟上,否则访问失败!
-------------------------------------------------------------------------------------
上面四种方式都是匹配的path路径后面加"/",下面说下path路径后面不带"/"的情况:
1)第一种情况,proxy_pass后面url带"/":
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy {
proxy_pass http://192.168.1.5:8090/;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service2)第二种情况,proxy_pass后面url不带"/"
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy {
proxy_pass http://192.168.1.5:8090;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]#这样配置的话,访问http://103.110.186.23/proxy会自动加上"/”(即变成http://103.110.186.23/proxy/),代理到192.168.1.5:8090/proxy/
3)第三种情况
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy {
proxy_pass http://192.168.1.5:8090/haha/;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service这样配置的话,访问http://103.110.186.23/proxy会自动加上"/”(即变成http://103.110.186.23/proxy/),代理到http://192.168.1.5:8090/haha/
4)第四种情况:相对于第三种配置的url不加"/"
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
location /proxy {
proxy_pass http://192.168.1.5:8090/haha;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
这样配置的话,访问http://103.110.186.23/proxy,和第三种结果一样,同样被代理到http://192.168.1.5:8090/haha/
# nginx
# 就会
# 第三种
# 不带
# 不加
# 相对于
# 第二种
# 第一种
# 都是
# 第四种
# 也要
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel项目如何进行性能优化_Laravel应用性能分析与优化技巧大全
详解Android——蓝牙技术 带你实现终端间数据传输
详解Android中Activity的四大启动模式实验简述
制作企业网站建设方案,怎样建设一个公司网站?
Java解压缩zip - 解压缩多个文件或文件夹实例
Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】
百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏
如何用狗爹虚拟主机快速搭建网站?
网页制作模板网站推荐,网页设计海报之类的素材哪里好?
个人网站制作流程图片大全,个人网站如何注销?
Laravel怎么配置自定义表前缀_Laravel数据库迁移与Eloquent表名映射【步骤】
php读取心率传感器数据怎么弄_php获取max30100的心率值【指南】
JS实现鼠标移上去显示图片或微信二维码
如何有效防御Web建站篡改攻击?
Laravel Debugbar怎么安装_Laravel调试工具栏配置指南
Bootstrap CSS布局之列表
成都品牌网站制作公司,成都营业执照年报网上怎么办理?
Swift开发中switch语句值绑定模式
实例解析Array和String方法
音响网站制作视频教程,隆霸音响官方网站?
合肥制作网站的公司有哪些,合肥聚美网络科技有限公司介绍?
瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口
Python高阶函数应用_函数作为参数说明【指导】
HTML透明颜色代码怎么让图片透明_给img元素加透明色的技巧【方法】
php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】
如何快速辨别茅台真假?关键步骤解析
悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】
Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制
怎样使用JSON进行数据交换_它有什么限制
如何做网站制作流程,*游戏网站怎么搭建?
Laravel怎么生成二维码图片_Laravel集成Simple-QrCode扩展包与参数设置【实战】
Laravel如何处理JSON字段_Eloquent原生JSON字段类型操作教程
Laravel如何升级到最新的版本_Laravel版本升级流程与兼容性处理
Laravel怎么实现API接口鉴权_Laravel Sanctum令牌生成与请求验证【教程】
海南网站制作公司有哪些,海口网是哪家的?
如何用y主机助手快速搭建网站?
Win11怎么关闭专注助手 Win11关闭免打扰模式设置【操作】
javascript中数组(Array)对象和字符串(String)对象的常用方法总结
Angular 表单中正确绑定输入值以确保提交与验证正常工作
b2c电商网站制作流程,b2c水平综合的电商平台?
香港服务器部署网站为何提示未备案?
Laravel怎么上传文件_Laravel图片上传及存储配置
Laravel如何使用Eloquent进行子查询
Laravel如何实现图片防盗链功能_Laravel中间件验证Referer来源请求【方案】
javascript读取文本节点方法小结
Laravel如何生成URL和重定向?(路由助手函数)
Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】
如何在建站之星绑定自定义域名?
Win11怎样安装网易有道词典_Win11安装词典教程【步骤】
Laravel怎么清理缓存_Laravel optimize clear命令详解


ot@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]# curl http://192.168.1.23/proxy/
192.168.1.5 haha-index.html