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 】
相关推荐:
悟空识字怎么关闭自动续费_悟空识字取消会员自动扣费步骤
PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)
Claude怎样写结构化提示词_Claude结构化提示词写法【教程】
国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?
详解jQuery停止动画——stop()方法的使用
如何用景安虚拟主机手机版绑定域名建站?
宙斯浏览器视频悬浮窗怎么开启 边看视频边操作其他应用教程
HTML透明颜色代码怎么让下拉菜单透明_下拉菜单透明背景指南【技巧】
Laravel如何实现图片防盗链功能_Laravel中间件验证Referer来源请求【方案】
动图在线制作网站有哪些,滑动动图图集怎么做?
中山网站制作网页,中山新生登记系统登记流程?
创业网站制作流程,创业网站可靠吗?
Laravel N+1查询问题如何解决_Eloquent预加载(Eager Loading)优化数据库查询
桂林网站制作公司有哪些,桂林马拉松怎么报名?
uc浏览器二维码扫描入口_uc浏览器扫码功能使用地址
JS碰撞运动实现方法详解
如何快速搭建高效WAP手机网站吸引移动用户?
Laravel如何使用Laravel Vite编译前端_Laravel10以上版本前端静态资源管理【教程】
胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?
Laravel如何配置中间件Middleware_Laravel自定义中间件拦截请求与权限校验【步骤】
Laravel Admin后台管理框架推荐_Laravel快速开发后台工具
浅谈javascript alert和confirm的美化
如何利用DOS批处理实现定时关机操作详解
Laravel Debugbar怎么安装_Laravel调试工具栏配置指南
Laravel如何使用Service Provider注册服务_Laravel服务提供者配置与加载
网站制作大概多少钱一个,做一个平台网站大概多少钱?
Laravel如何使用Socialite实现第三方登录?(微信/GitHub示例)
Laravel如何设置自定义的日志文件名_Laravel根据日期或用户ID生成动态日志【技巧】
个人摄影网站制作流程,摄影爱好者都去什么网站?
实例解析Array和String方法
佐糖AI抠图怎样调整抠图精度_佐糖AI精度调整与放大细化操作【攻略】
如何快速搭建安全的FTP站点?
Laravel如何使用API Resources格式化JSON响应_Laravel数据资源封装与格式化输出
Laravel如何升级到最新版本?(升级指南和步骤)
浅述节点的创建及常见功能的实现
图册素材网站设计制作软件,图册的导出方式有几种?
如何快速生成专业多端适配建站电话?
Laravel中Service Container是做什么的_Laravel服务容器与依赖注入核心概念解析
如何获取上海专业网站定制建站电话?
如何在Windows 2008云服务器安全搭建网站?
Laravel Pest测试框架怎么用_从PHPUnit转向Pest的Laravel测试教程
JavaScript如何操作视频_媒体API怎么控制播放
Laravel如何配置.env文件管理环境变量_Laravel环境变量使用与安全管理
高端网站建设与定制开发一站式解决方案 中企动力
如何在香港服务器上快速搭建免备案网站?
手机网站制作与建设方案,手机网站如何建设?
Laravel怎么导出Excel文件_Laravel Excel插件使用教程
Laravel如何创建和注册中间件_Laravel中间件编写与应用流程
夸克浏览器网页跳转延迟怎么办 夸克浏览器跳转优化
laravel怎么为API路由添加签名中间件保护_laravel API路由签名中间件保护方法


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