1.前提
虚拟机能连接外网
仿真http应用需在本虚拟机启用(原因:只有一台虚拟机做测试)
http_8080和http_8081要启用(http测试应用)
[root@cent79-2 ~]# ls -l http_*
-rwxr-xr-x 1 root root 6391676 Jul 19 13:39 http_8080
-rwxr-xr-x 1 root root 6391676 Jul 19 13:39 http_8081
[root@cent79-2 ~]# ./http_8080 &
[1] 1490
[root@cent79-2 ~]# ./http_8081 &
[2] 1496
[root@cent79-2 ~]# netstat -antulp |grep 8080
tcp6 0 0 :::8080 :::* LISTEN 1490/./http_8080
[root@cent79-2 ~]# netstat -antulp |grep 8081
tcp6 0 0 :::8081 :::* LISTEN 1496/./http_8081
[root@cent79-2 ~]# curl 192.168.10.156:8080
{"text":"I am one!"}
[root@cent79-2 ~]# curl 192.168.10.156:8081
{"text":"I am two!"}
[root@cent79-2 ~]#
2.Ngnix配置反向代理
2.1.Nginx配置单个反向代理
1>.编辑nginx配置文件nginx.conf,添加反向代理配置
user nginx nginx;
worker_processes 2;
worker_rlimit_nofile 1024;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 1024;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
autoindex on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name 192.168.10.156;
location / {
proxy_pass http://www.baidu.com;
}
}
}
|
2>.nginx语法验证
命令:
nginx -t
[root@cent79-2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@cent79-2 ~]#
|
3>.重启nginx
命令:
systemctl restart nginx
systemctl status nginx
[root@cent79-2 ~]# systemctl restart nginx
[root@cent79-2 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2023-07-20 20:16:16 CST; 3s ago
Docs: http://nginx.org/en/docs/
Process: 2504 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
Process: 2509 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2510 (nginx)
CGroup: /system.slice/nginx.service
├─2510 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
├─2511 nginx: worker process
└─2512 nginx: worker process
Jul 20 20:16:15 cent79-2 systemd[1]: Starting nginx - high performance web server...
Jul 20 20:16:16 cent79-2 systemd[1]: Started nginx - high performance web server.
[root@cent79-2 ~]#
|
4>.反向代理验证
地址:
http://192.168.10.156

转向为:

2.2.Nginx配置反向代理集群
1>.编辑nginx配置文件nginx.conf,添加反向代理配置
user nginx nginx;
worker_processes 2;
worker_rlimit_nofile 1024;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 1024;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
autoindex on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
upstream www.ztj.com {
server 192.168.10.156:8080 weight=1 max_fails=2 fail_timeout=15s;
server 192.168.10.156:8081 weight=1 max_fails=2 fail_timeout=15s;
}
server {
listen 80;
server_name 192.168.10.156;
location / {
proxy_pass http://www.ztj.com;
}
}
}
|
2>.nginx语法验证
命令:
nginx -t
[root@cent79-2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@cent79-2 ~]#
|
3>.重启nginx
命令:
systemctl restart nginx
systemctl status nginx
[root@cent79-2 ~]# systemctl restart nginx
[root@cent79-2 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2023-07-20 20:32:50 CST; 4s ago
Docs: http://nginx.org/en/docs/
Process: 2673 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
Process: 2678 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2679 (nginx)
CGroup: /system.slice/nginx.service
├─2679 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
├─2680 nginx: worker process
└─2681 nginx: worker process
Jul 20 20:32:50 cent79-2 systemd[1]: Stopped nginx - high performance web server.
Jul 20 20:32:50 cent79-2 systemd[1]: Starting nginx - high performance web server...
Jul 20 20:32:50 cent79-2 systemd[1]: Started nginx - high performance web server.
[root@cent79-2 ~]#
|
4>.反向代理集群验证
命令:curl 192.168.10.156
[root@cent79-2 ~]# curl 192.168.10.156
{"text":"I am one!"}
[root@cent79-2 ~]# curl 192.168.10.156
{"text":"I am two!"}
[root@cent79-2 ~]#
|
2.3.Nginx配置基于context的反向代理(重点)
1>.编辑nginx配置文件nginx.conf,添加反向代理配置
user nginx nginx;
worker_processes 2;
worker_rlimit_nofile 1024;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 1024;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
autoindex on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name 192.168.10.156;
location / {
proxy_pass http://www.baidu.com;
}
location /root {
proxy_pass http://127.0.0.1:8080;
location /root/api {
proxy_pass http://127.0.0.1:8081;
}
}
}
}
|
2>.nginx语法验证
命令:
nginx -t
[root@cent79-2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@cent79-2 ~]#
|
3>.重启nginx
命令:
systemctl restart nginx
systemctl status nginx
[root@cent79-2 ~]# systemctl restart nginx
[root@cent79-2 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2023-07-20 20:39:33 CST; 2s ago
Docs: http://nginx.org/en/docs/
Process: 2713 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
Process: 2718 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2719 (nginx)
CGroup: /system.slice/nginx.service
├─2719 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
├─2720 nginx: worker process
└─2721 nginx: worker process
Jul 20 20:39:33 cent79-2 systemd[1]: Starting nginx - high performance web server...
Jul 20 20:39:33 cent79-2 systemd[1]: Started nginx - high performance web server.
[root@cent79-2 ~]#
|
4>.反向代理context验证
http://192..168.10.156

http://192..168.10.156/root

http://192..168.10.156/root/api
