首页 运维 nginx nginx proxy_pass反向代理配置实例分析

nginx proxy_pass反向代理配置实例分析

May 13, 2023 pm 11:19 PM
nginx proxy_pass

下面举个小实例说明下:

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

nginx proxy_pass反向代理配置实例分析

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/proxy
<html>
<head><title>301 moved permanently</title></head>
<body bgcolor="white">
<center><h1>301 moved permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

页面访问http://103.110.186.23/proxy的时候,会自动加上"/”(同理是由于proxy_pass配置的url后面加了"/"),并反代到http://103.110.186.5:8090的结果

nginx proxy_pass反向代理配置实例分析

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/

nginx proxy_pass反向代理配置实例分析

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
[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/

nginx proxy_pass反向代理配置实例分析

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文件也要跟上,否则访问失败!

nginx proxy_pass反向代理配置实例分析

-------------------------------------------------------------------------------------
上面四种方式都是匹配的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.service

nginx proxy_pass反向代理配置实例分析

nginx proxy_pass反向代理配置实例分析

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
[root@localhost conf.d]#

这样配置的话,访问http://103.110.186.23/proxy会自动加上"/”(即变成http://103.110.186.23/proxy/),代理到192.168.1.5:8090/proxy/

nginx proxy_pass反向代理配置实例分析

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/

nginx proxy_pass反向代理配置实例分析

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

nginx proxy_pass反向代理配置实例分析

这样配置的话,访问http://103.110.186.23/proxy,和第三种结果一样,同样被代理到http://192.168.1.5:8090/haha/

以上是nginx proxy_pass反向代理配置实例分析的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

PHP教程
1535
276
php写完代码怎么执行 php代码执行的几种常见方式 php写完代码怎么执行 php代码执行的几种常见方式 May 23, 2025 pm 08:33 PM

PHP代码可以通过多种方式执行:1.使用命令行,直接输入“php文件名”执行脚本;2.通过Web服务器,将文件放入文档根目录并通过浏览器访问;3.在IDE中运行,利用内置调试工具;4.使用在线PHP沙箱或代码执行平台进行测试。

安装Nginx后配置文件路径及初始设置 安装Nginx后配置文件路径及初始设置 May 16, 2025 pm 10:54 PM

了解Nginx的配置文件路径和初始设置非常重要,因为它是优化和管理Web服务器的第一步。1)配置文件路径通常是/etc/nginx/nginx.conf,使用nginx-t命令可以查找并测试语法。2)初始设置包括全局设置(如user、worker_processes)和HTTP设置(如include、log_format),这些设置允许根据需求进行定制和扩展,错误配置可能导致性能问题和安全漏洞。

linux如何限制用户资源?ulimit怎么配置? linux如何限制用户资源?ulimit怎么配置? May 29, 2025 pm 11:09 PM

Linux系统通过ulimit命令限制用户资源,防止资源过度占用。1.ulimit是shell内置命令,可限制文件描述符数(-n)、内存大小(-v)、线程数(-u)等,分为软限制(当前生效值)和硬限制(最高上限)。2.临时修改直接使用ulimit命令,如ulimit-n2048,但仅对当前会话有效。3.永久生效需修改/etc/security/limits.conf及PAM配置文件,并添加sessionrequiredpam_limits.so。4.systemd服务需在unit文件中设置Lim

Debian Nginx配置技巧有哪些 Debian Nginx配置技巧有哪些 May 29, 2025 pm 11:06 PM

在Debian系统上配置Nginx时,以下是一些实用的技巧:配置文件的基本结构全局设置部分:定义影响整个Nginx服务的行为参数,比如工作线程数量及运行用户权限。事件处理部分:决定Nginx如何应对网络连接,是提升性能的关键配置。HTTP服务部分:包含大量与HTTP服务相关的设定,可内嵌多个server和location块。核心配置选项worker_connections:定义每个工作线程所能处理的最大连接数,通常设为1024。multi_accept:激活多连接接收模式,增强并发处理的能力。s

配置Nginx服务开机自启动的具体步骤 配置Nginx服务开机自启动的具体步骤 May 16, 2025 pm 10:39 PM

Nginx配置开机自启动的步骤如下:1.创建systemd服务文件:sudonano/etc/systemd/system/nginx.service,并添加相关配置。2.重新加载systemd配置:sudosystemctldaemon-reload。3.启用Nginx开机自启动:sudosystemctlenablenginx。通过这些步骤,Nginx会在系统启动时自动运行,确保网站或应用的可靠性和用户体验。

配置PhpStorm与Docker容器化开发环境 配置PhpStorm与Docker容器化开发环境 May 20, 2025 pm 07:54 PM

通过Docker容器化技术,PHP开发者可以利用PhpStorm提高开发效率和环境一致性。具体步骤包括:1.创建Dockerfile定义PHP环境;2.在PhpStorm中配置Docker连接;3.创建DockerCompose文件定义服务;4.配置远程PHP解释器。优点是环境一致性强,缺点包括启动时间长和调试复杂。

Debian Apache2的SEO优化技巧有哪些 Debian Apache2的SEO优化技巧有哪些 May 28, 2025 pm 05:03 PM

DebianApache2的SEO优化技巧涵盖多个层面,以下是一些关键方法:关键词研究:利用工具(如关键词魔术工具)挖掘页面的核心及辅助关键词。优质内容创作:产出有价值且原创的内容,内容需经过深入调研,确保语言流畅且格式清晰。内容排版与结构优化:运用标题和小标题引导阅读。编写简洁明了的段落和句子。利用列表展示重点信息。结合图片、视频等多媒体增强表现力。留白设计提升文本易读性。技术层面SEO改进:robots.txt文件:规定搜索引擎爬虫的访问权限。加速网页加载:借助缓存机制和Apache配置优化

如何在Debian上实现Docker的自动化部署 如何在Debian上实现Docker的自动化部署 May 28, 2025 pm 04:33 PM

在Debian系统上实现Docker的自动化部署可以通过多样的方法来完成,以下是详细的步骤指南:1.安装Docker首先,确保你的Debian系统保持最新状态:sudoaptupdatesudoaptupgrade-y接着,安装必要的软件包以支持APT通过HTTPS访问仓库:sudoaptinstallapt-transport-httpsca-certificatescurlsoftware-properties-common-y导入Docker的官方GPG密钥:curl-

See all articles