当前位置:   article > 正文

nginx虚拟主机配置、主机用户访问控制配置、反向代理后实现nginx和apache两种web服务器能够记录客户端的真实IP地址配置演示_nginx配置真实ip

nginx配置真实ip

目录

一.静态页面

1.更改页面内容

2.更改配置文件

3.测试

 二.虚拟主机配置

1.基于IP

(1)在html目录下新建目录存放测试文件

(2)修改nginx.conf文件,在htttp模块中配置两个server模块分别对应两个IP

(3)测试

2.基于端口

(1)创建测试界面

(2)同样在http模块中添加server模块,注意工作目录的变动

(3)测试

 3.基于域名

(1)在前面基于IP的基础上添加server_name一行即可

(2)修改/etc/hosts文件

(3)测试

三.nginx错误日志和访问日志配置

1. 在server模块中指定日志类型和日志存放目录即可

2.测试两边后查看

四.基于主机访问控制

1.修改nginx.conf文件

2.到其他主机上测试

(1)191主机

(2)180主机

五.基于用户访问控制

1.修改nginx.conf文件

2.使用hpasswd为用户创建密码文件,并指定到刚才指定的密码文件webck

3.测试

六.缓存配置

七.配置服务器记录真实IP地址

1.构建环境

2.配置反向代理

 3.实现两台web服务器记录真实客户端IP地址

(1)Nginx代理服务器配置

(2)Apache类的web服务器


一.静态页面

要关闭apache的httpd

  1. [root@localhost conf]# systemctl status httpd.service
  2. ● httpd.service - The Apache HTTP Server
  3. Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  4. Active: inactive (dead)
  5. Docs: man:httpd(8)
  6. man:apachectl(8)

1.更改页面内容

找到自己的nginx安装目录里的html目录,修改index.html

  1. [root@localhost html]# pwd
  2. /usr/local/src/nginx-1.22.0/html
  3. [root@localhost html]# cat index.html
  4. <h1>nginx</h1>

2.更改配置文件

 nginx安装目录里的nginx.conf文件

  1. [root@localhost conf]# pwd
  2. /usr/local/src/nginx-1.22.0/conf
  3. [root@localhost conf]# vim nginx.conf

找到server内的区块位置,更改工作目录和指定html文件 

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. #charset koi8-r;
  5. #access_log logs/host.access.log main;
  6. location / {
  7. root /usr/local/src/nginx-1.22.0/html; #你的html文件存放目录
  8. index index.html; #如果前面页面文件是其它名字,这里要更改一致
  9. }
  10. #error_page 404 /404.html;
  11. # redirect server error pages to the static page /50x.html
  12. #
  13. error_page 500 502 503 504 /50x.html;
  14. location = /50x.html {
  15. root html;
  16. }
  17. }
  18. [root@localhost conf]# systemctl restart nginx

057dca4f870042a99d556089b77306dd.png

3.测试

503b74fcd1a44135b90eed1f872e9003.png

 二.虚拟主机配置

1.基于IP

(1)在html目录下新建目录存放测试文件

  1. [root@localhost html]# ll
  2. total 8
  3. -rw-r--r-- 1 1001 1001 497 May 24 2022 50x.html
  4. -rw-r--r-- 1 1001 1001 15 Aug 10 10:13 index.html
  5. drwxr-xr-x 2 root root 43 Aug 10 11:53 ip
  6. drwxr-xr-x 2 root root 6 Aug 10 11:51 port
  7. [root@localhost html]# pwd
  8. /usr/local/src/nginx-1.22.0/html
  9. [root@localhost html]# cat ip/index.html ip/index1.html
  10. 190
  11. 195

(2)修改nginx.conf文件,在htttp模块中配置两个server模块分别对应两个IP

  1. server {
  2. listen 192.168.2.190;
  3. location / {
  4. root /usr/local/src/nginx-1.22.0/html/ip;
  5. index index.html index.htm;
  6. }
  7. }
  8. server {
  9. listen 192.168.2.195;
  10. location / {
  11. root /usr/local/src/nginx-1.22.0/html/ip;
  12. index index1.html index.htm;
  13. }
  14. }
  15. [root@localhost conf]# systemctl restart nginx

(3)测试

882c60b7f0474e34929b7e4d6bcfd0f4.png

 1f03213f5bd7406d8baa044b69930e90.png

2.基于端口

(1)创建测试界面

  1. [root@localhost html]# cat port/index.html port/index1.html
  2. 8090
  3. 8099

(2)同样在http模块中添加server模块,注意工作目录的变动

  1. server {
  2. listen 8090;
  3. location / {
  4. root /usr/local/src/nginx-1.22.0/html/port;
  5. index index.html index.htm;
  6. }
  7. }
  8. server {
  9. listen 8099;
  10. location / {
  11. root /usr/local/src/nginx-1.22.0/html/port;
  12. index index1.html index.htm;
  13. }
  14. }
  15. [root@localhost conf]# systemctl restart nginx

(3)测试

0cb93d28ad46430284142b21bae21668.png

bdcb4441fb1141058883dc3520a7e4ba.png

 3.基于域名

(1)在前面基于IP的基础上添加server_name一行即可

  1. server {
  2. listen 192.168.2.190;
  3. server_name www.aabb.com;
  4. location / {
  5. root /usr/local/src/nginx-1.22.0/html/ip;
  6. index index.html index.htm;
  7. }
  8. }
  9. server {
  10. listen 192.168.2.195;
  11. server_name www.llss.com;
  12. location / {
  13. root /usr/local/src/nginx-1.22.0/html/ip;
  14. index index1.html index.htm;
  15. }
  16. }
  17. [root@localhost conf]# systemctl restart nginx

(2)修改/etc/hosts文件

Linux

  1. [root@localhost conf]# vim /etc/hosts
  2. 192.168.2.190 www.aabb.com
  3. 192.168.2.195 www.llss.com

windows

通过powershell使用notepad修改hosts文件

  1. Windows PowerShell
  2. 版权所有(C) Microsoft Corporation。保留所有权利。
  3. 安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows
  4. PS C:\WINDOWS\system32> cd .\drivers\etc\
  5. PS C:\WINDOWS\system32\drivers\etc> notepad .\hosts
  6. PS C:\WINDOWS\system32\drivers\etc>

c046652771624e63941ac79430d6f945.png

(3)测试

a5732732f14c404db2712b4703ed4996.png

e6e984ec688e4813b278e03ee9658a43.png

三.nginx错误日志和访问日志配置

1. 在server模块中指定日志类型和日志存放目录即可

2.测试两边后查看

  1. [root@localhost logs]# cat access.log
  2. 192.168.2.2 - - [10/Aug/2023:14:06:00 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.200"
  3. 192.168.2.2 - - [10/Aug/2023:14:06:00 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.2.190/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.200"
  4. [root@localhost logs]# cat error.log
  5. 2023/08/10 14:06:00 [error] 59751#59751: *1 open() "/usr/local/src/nginx-1.22.0/html/ip/favicon.ico" failed (2: No such file or directory), client: 192.168.2.2, server: www.aabb.com, request: "GET /favicon.ico HTTP/1.1", host: "192.168.2.190", referrer: "http://192.168.2.190/"
  6. [root@localhost logs]# pwd
  7. /usr/local/src/nginx-1.22.0/logs

四.基于主机访问控制

1.修改nginx.conf文件

修改server区块内的location块,deny表示禁止这台主机访问,allow表示允许,可以使用all表示禁止/允许所有,一定要注意匹配顺序是从上至下,下例也就表示允许除2.191外的主机访问

  1. server {
  2. listen 192.168.2.190;
  3. server_name www.aabb.com;
  4. error_log /usr/local/src/nginx-1.22.0/logs/error.log;
  5. access_log /usr/local/src/nginx-1.22.0/logs/access.log;
  6. location / {
  7. root /usr/local/src/nginx-1.22.0/html/ip;
  8. index index.html index.htm;
  9. deny 192.168.2.191;
  10. allow all;
  11. #deny 192.168.2.0/24; 可以指定一个网段
  12. }
  13. }
  14. [root@localhost conf]# systemctl restart nginx.service

2.到其他主机上测试

(1)191主机

  1. [root@localhost ~]# ip a | grep ens33
  2. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  3. inet 192.168.2.191/24 brd 192.168.2.255 scope global noprefixroute ens33
  4. [root@localhost ~]# curl 192.168.2.190
  5. <html>
  6. <head><title>403 Forbidden</title></head>
  7. <body>
  8. <center><h1>403 Forbidden</h1></center>
  9. <hr><center>nginx/1.22.0</center>
  10. </body>
  11. </html>

(2)180主机

  1. [root@localhost ~]# ip a | grep ens160
  2. 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  3. inet 192.168.2.180/24 brd 192.168.2.255 scope global noprefixroute ens160
  4. [root@localhost ~]# curl 192.168.2.190
  5. 190

五.基于用户访问控制

1.修改nginx.conf文件

  1. server {
  2. listen 192.168.2.190;
  3. server_name www.aabb.com;
  4. error_log /usr/local/src/nginx-1.22.0/logs/error.log;
  5. access_log /usr/local/src/nginx-1.22.0/logs/access.log;
  6. location / {
  7. root /usr/local/src/nginx-1.22.0/html/ip;
  8. index index.html index.htm;
  9. allow all;
  10. auth_basic "here"; #加上这两行,这行是出现在第一次访问Nginx站点的弹出框内
  11. auth_basic_user_file /usr/local/src/nginx-1.22.0/conf/webck; #指定密码文件
  12. }
  13. }
  14. [root@localhost conf]# pwd
  15. /usr/local/src/nginx-1.22.0/conf
  16. [root@localhost conf]# ll | grep webck
  17. -rw-r--r-- 1 root root 22 Aug 10 18:54 webck

2.使用hpasswd为用户创建密码文件,并指定到刚才指定的密码文件webck

  1. [root@localhost conf]# htpasswd -c -d /usr/local/src/nginx-1.22.0/conf/webck sulibao
  2. New password:
  3. Re-type new password:
  4. Warning: Password truncated to 8 characters by CRYPT algorithm.
  5. Adding password for user sulibao
  6. [root@localhost conf]# cat webck
  7. sulibao:8sWMglgDCLsGQ #密码已加密
  8. [root@localhost conf]# systemctl restart nginx

3.测试

c4e181c3f7ce4e26904d9d8e02050d9c.png

 1548f1b50628489f893b087505fd1ac7.png

六.缓存配置

expires参数,下例表示对以这些后缀结尾的图片文件缓存30天

  1. server {
  2. listen 192.168.2.190;
  3. server_name www.aabb.com;
  4. error_log /usr/local/src/nginx-1.22.0/logs/error.log;
  5. access_log /usr/local/src/nginx-1.22.0/logs/access.log;
  6. location / {
  7. root /usr/local/src/nginx-1.22.0/html/ip;
  8. index index.html index.htm;
  9. allow all;
  10. auth_basic "here";
  11. auth_basic_user_file /usr/local/src/nginx-1.22.0/conf/webck;
  12. }
  13. location ~ .*\.(gif|jpg|png)$ {
  14. expires 30d;
  15. }
  16. }

七.配置服务器记录真实IP地址

1.构建环境

三台设备,设备1(2.190)部署nginx环境,设备2(2.191)部署nginx,设备3(2.193)apache环境,保证都有区别的web页面内容

2.配置反向代理

(1)基于源码安装的nginx环境下修改nginx.conf(设备1)

  1. [root@localhost conf]# pwd
  2. /usr/local/src/nginx-1.22.0/conf
  3. [root@localhost conf]# vim nginx.conf
  4. worker_processes  1;
  5. events {
  6.     worker_connections  1024;
  7. }
  8. http {
  9.     include       mime.types;
  10.     default_type  application/octet-stream;
  11.     sendfile        on;
  12.     keepalive_timeout  65;
  13.     error_log /usr/local/src/nginx-1.22.0/logs/error.log;
  14.     access_log /usr/local/src/nginx-1.22.0/logs/access.log;
  15.         server {
  16.                 listen 80;
  17.                 server_name www.aabb.com;
  18.                 location / {
  19.                         proxy_pass http://192.168.2.191;
  20.                 }
  21.         }               
  22.         server {
  23.                 listen 80;
  24.                 server_name www.llss.com;
  25.                 location / {
  26.                         proxy_pass http://192.168.2.193;
  27.                 }
  28.         }
  29. }

(2)通过windows powershell进行修改hosts文件并测试

  1. PS C:\WINDOWS\system32> cd .\drivers\etc\
  2. PS C:\WINDOWS\system32\drivers\etc> notepad .\hosts
  3. PS C:\WINDOWS\system32\drivers\etc>

dded7aa3d8c94121a33ad8d6322871a3.png

(3)设备2和设备3上查看日志,可以看到访问来源都是代理服务器(2.190)而不是真实客户端地址

3e7eb24b0490475e9f1e8c91c52f5767.png

 3.实现两台web服务器记录真实客户端IP地址

(1)Nginx代理服务器配置

设备1,在proxy_pass后继续添加以下内容

  1. server {
  2.                 listen 80;
  3.                 server_name www.aabb.com;
  4.                 location / {
  5.                         proxy_pass http://192.168.2.191;
  6.                         proxy_set_header Host $host;
  7.                         proxy_set_header X-Real-IP $remote_addr;
  8.                         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9.                         proxy_set_header X-Forwarded-Proto $scheme;
  10.                 }
  11.         }
  12. server {
  13.                 listen 80;
  14.                 server_name www.llss.com;
  15.                 location / {
  16.                         proxy_pass http://192.168.2.193;
  17.                         proxy_set_header Host $host;
  18.                         proxy_set_header X-Forwarded-For $http_x_forwarded_for;
  19.                 }
  20.         }

设备2,修改nginx.conf文件,在index下方继续添加以下内容

  1. server {
  2.         listen       80;
  3.         server_name  localhost;
  4.         access_log   logs/access.log;
  5.         location / {
  6.             root   /usr/local/src/nginx-1.22.0/html;
  7.             index  index.html index.htm;
  8.             set_real_ip_from  192.168.2.190; #此处填写你的nginx代理服务器的地址
  9.             real_ip_header    X-Forwarded-For;
  10.             real_ip_recursive on;
  11.         }

浏览器访问后设备2查看日志

通过浏览器访问传回虚拟机的这个访问地址是windows上的虚拟网卡VMnet8的地址

747fe133b40f49ae86d49d2954cea3c7.png

设备3做域名映射后访问设备2查看日志以进一步验证

  1. #修改/etc/hosts文件后查看
  2. [root@localhost ~]# tail -1 /etc/hosts
  3. 192.168.2.190 www.aabb.com
  4. [root@localhost ~]# curl www.aabb.com
  5. 191

89e9a4890b1841899f6221930bbbe735.png

(2)Apache类的web服务器

设备3,修改httpd.conf文件

  1. [root@localhost conf]# pwd
  2. /etc/httpd/conf
  3. [root@localhost conf]# vim httpd.conf

19d25b6b96564004b6a73d820a128489.png

把原本这里的第一行LogFormat换成这个

  1. LogFormat "%{X-FORWARDED-FOR}i %h %l %u %t %r %>s %b %{Referer}i %{User-Agent}i" combined
  2. [root@localhost conf]# systemctl restart httpd.service

浏览器访问设备3进行测试

返回了真实地址和nginx代理服务器的真实地址 

aecdb745f1d546a1b77e03890b2c4408.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/690072
推荐阅读
相关标签
  

闽ICP备14008679号