nginx代理模块怎么使用
nginx 代理模块
说明:代理模块的指令有很多我这里只讲解重要的proxy_pass,想了解更多代理指令请参考官方中文文档。
这个模块可以转发请求到其他的服务器。http/1.0无法使用keepalive(后端服务器将为每个请求创建并且删除连接)。nginx为浏览器发送http/1.1并为后端服务器发送http/1.0,这样浏览器就可以为浏览器处理keepalive。
如下例:
location / { proxy_pass http://localhost:8000; proxy_set_header x-real-ip $remote_addr; }
注意,当使用http proxy模块(甚至fastcgi),所有的连接请求在发送到后端服务器之前nginx将缓存它们,因此,在测量从后端传送的数据时,它的进度显示可能不正确。
实验拓扑:
7.配置http反向代理
[root@nginx ~]# cd /etc/nginx/ [root@nginx nginx]# cp nginx.conf nginx.conf.bak #备份一个原配置文件 [root@nginx nginx]# vim nginx.conf location / { proxy_pass http://192.168.18.201; }
指令说明:proxy_pass
语法:proxy_pass url
默认值:no
使用字段:location, location中的if字段
这个指令设置被代理服务器的地址和被映射的uri,地址可以使用主机名或ip加端口号的形式,例如:proxy_pass http://localhost:8000/uri/;
8.重新加载一下配置文件
[root@nginx ~]# service nginx reload nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 重新载入 nginx: [确定]
9.测试一下
注,大家可以看到,当我们访问192.168.18.208时,被代理重新定向到web1上。
10.查看一下web服务器日志
[root@web1 ~]# tail /var/log/httpd/access_log 192.168.18.208 - - [04/sep/2013:00:14:20 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36" 192.168.18.208 - - [04/sep/2013:00:14:20 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36" 192.168.18.208 - - [04/sep/2013:00:14:20 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36" 192.168.18.138 - - [04/sep/2013:00:14:45 +0800] "get / http/1.1" 200 23 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36" 192.168.18.138 - - [04/sep/2013:00:14:48 +0800] "get /favicon.ico http/1.1" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36" 192.168.18.208 - - [04/sep/2013:00:14:55 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36" 192.168.18.208 - - [04/sep/2013:00:15:05 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36" 192.168.18.208 - - [04/sep/2013:00:15:13 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36" 192.168.18.208 - - [04/sep/2013:00:15:16 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36" 192.168.18.208 - - [04/sep/2013:00:15:16 +0800] "get /favicon.ico http/1.0" 404 289 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36"
注,大家可以看到我们这里的客户的ip全是,nginx代理服务器的ip,并不是真实客户端的ip。下面我们修改一下,让日志的ip显示真实的客户端的ip。
11.修改nginx配置文件
location / { proxy_pass http://192.168.18.201; proxy_set_header x-real-ip $remote_addr; #加上这一行 }
指令说明:proxy_set_header
语法:proxy_set_header header value
默认值: host and connection
使用字段:http, server, location
这个指令允许将发送到被代理服务器的请求头重新定义或者增加一些字段。这个值可以是一个文本,变量或者它们的组合。proxy_set_header在指定的字段中没有定义时会从它的上级字段继承。
12.重新加载一下配置文件
[root@nginx ~]# service nginx reload nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 重新载入 nginx: [确定]
13.测试并查看日志
[root@web1 ~]# tail /var/log/httpd/access_log 192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.208 - - [03/sep/2013:16:26:18 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
注,大家可以看到日志记录的还是代理的ip,没有显示真实客户端的ip,为什么呢?我们来看一下httpd的配置文件。
14.查看并修改httpd配置文件
[root@web1 ~]# vim /etc/httpd/conf/httpd.conf
注,大家可以这里记录日志的参数还是%h,下面我们修改一下参数。
注,这是修改后的参数,将h%修改为%{x-real-ip}i,好的下面我们再来测试一下。
15.重启并测试
[root@web1 ~]# service httpd restart 停止 httpd: [确定] 正在启动 httpd: [确定] [root@web1 ~]# tail /var/log/httpd/access_log 192.168.18.138 - - [03/sep/2013:17:09:14 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.138 - - [03/sep/2013:17:09:14 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)" 192.168.18.138 - - [03/sep/2013:17:09:15 +0800] "get / http/1.0" 200 23 "-" "mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; wow64; trident/6.0)"
注,大家可以看到现在的日志里记录的ip地址就是真实的客户端地址了。
以上是nginx代理模块怎么使用的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

要让 Tomcat 服务器对外网访问,需要:修改 Tomcat 配置文件,允许外部连接。添加防火墙规则,允许访问 Tomcat 服务器端口。创建 DNS 记录,将域名指向 Tomcat 服务器公有 IP。可选:使用反向代理提升安全性和性能。可选:设置 HTTPS 以提高安全性。

Nginx 的启动和停止命令分别为 nginx 和 nginx -s quit。启动命令直接启动服务器,而停止命令优雅地关闭服务器,允许所有当前请求处理完毕。其他可用停止信号包括 stop 和 reload。

ThinkPHP Framework 的本地运行步骤:下载并解压 ThinkPHP Framework 到本地目录。创建虚拟主机(可选),指向 ThinkPHP 根目录。配置数据库连接参数。启动 Web 服务器。初始化 ThinkPHP 应用程序。访问 ThinkPHP 应用程序 URL 运行。

要注册 phpMyAdmin,需要先创建 MySQL 用户并授予其权限,然后下载、安装和配置 phpMyAdmin,最后登录到 phpMyAdmin 以管理数据库。

Node.js 项目的服务器部署步骤:准备部署环境:获取服务器访问权限、安装 Node.js、设置 Git 存储库。构建应用程序:使用 npm run build 生成可部署代码和依赖项。上传代码到服务器:通过 Git 或文件传输协议。安装依赖项:SSH 登录服务器并使用 npm install 安装应用程序依赖项。启动应用程序:使用 node index.js 等命令启动应用程序,或使用 pm2 等进程管理器。配置反向代理(可选):使用 Nginx 或 Apache 等反向代理路由流量到应用程

要解决 "Welcome to nginx!" 错误,需要检查虚拟主机配置,启用虚拟主机,重新加载 Nginx,如果无法找到虚拟主机配置文件,则创建默认页面并重新加载 Nginx,这样错误消息将消失,网站将正常显示。

访问网站出现 nginx,原因可能是:服务器维护、服务器繁忙、浏览器缓存、DNS 问题、防火墙阻止、网站错误配置、网络连接问题或网站已关闭。尝试以下解决方案:等待维护结束、非高峰时段访问、清除浏览器缓存、刷新 DNS 缓存、禁用防火墙或防病毒软件、联系网站管理员、检查网络连接或使用搜索引擎或 Web 存档查找其他网站副本。如果问题仍然存在,请与网站管理员联系。

Docker 环境中容器通信有五种方法:共享网络、Docker Compose、网络代理、共享卷、消息队列。根据隔离性和安全性需求,选择最合适的通信方法,例如利用 Docker Compose 简化连接或使用网络代理提高隔离性。
