将nginx作为反向代理服务器,将请求复制为两个
为了进一步确立、、、
利用nginx作为反向代理,将HTTP请求发送到两台WEB服务器。
保護是我們的責任。
在GCE上准备了3台服务器。※操作系统是CentOS7.4
1. nginx的WEB服务器
2. apache的WEB服务器(用于复制)
3. nginx的反向代理服务器
步驟
nginx的Web服务器
使用yum安装nginx。
[root@nginx-Web ~]# yum install nginx
安全性被完全忽略,暂时删除配置文件启动系统。
[root@nginx-Web ~]# vi /etc/nginx/nginx.conf
[root@nginx-Web ~]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx-Web ~]# systemctl start nginx
events {
worker_connections 1024;
}
http{
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
server{
listen 80 default;
server_name localhost;
access_log /var/log/nginx/nginx-Web-access.log;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
}
将index.html文件放在文档根目录,并尝试通过“nginx的WEB服务器IP”进行访问。

阿帕奇的WEB服务器。
使用yum安装的nginx,与不考虑安全性的启动状态相似,我会删除配置文件并启动。
[root@apache-Web ~]# yum install httpd
[root@apache-Web ~]# vi /etc/httpd/conf/httpd.conf
[root@apache-Web ~]# systemctl start httpd
ServerRoot “/etc/httpd”
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
DocumentRoot “/var/www/html”
<Directory “/var/www/html”>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
DirectoryIndex index.html
LogFormat “%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined
CustomLog “/var/log/httpd/apache-web-access.log” combined
TypesConfig /etc/mime.types

用于反向代理的nginx服务器
使用yum进行安装,并在基于为Web服务器创建的conf文件的基础上添加反向代理的配置并启动。
[root@nginx-proxy ~]# yum -y install
[root@nginx-proxy ~]# vi /etc/nginx/nginx.conf
[root@nginx-proxy ~]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
events {
worker_connections 1024;
}
http{
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
server{
listen 80 default;
server_name localhost;
access_log /var/log/nginx/nginx-proxy-access.log;
location / {
proxy_pass http://nginx的WEB服务器的IP; #这样设置反向代理
}
}
}

添加设置以复制请求。
[root@nginx-proxy ~]# vi /etc/nginx/nginx.conf
[root@nginx-proxy ~]# systemctl restart nginx
events {
worker_connections 1024;
}
http {
server {
listen 80 default;
server_name localhost;
access_log /var/log/nginx/admin_access.log;
location / {
proxy_pass http://nginx的WEB服务器的IP;
post_action @testmirror; #加载复制配置
}
#复制配置开始
location @testmirror {
internal;
proxy_pass http://apache的WEB服务器的IP;
}
}
}
我实际访问一下,看看访问日志。
アクセスした端末IP - - [24/Feb/2018:08:37:41 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0"
nginxのリバプロサーバのIP - - [24/Feb/2018:08:37:41 +0000] "GET / HTTP/1.0" 200 27 "-" "Mozilla/5.0(Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0" "
-"
nginxのリバプロサーバのIP - - [24/Feb/2018:08:37:41 +0000] "GET / HTTP/1.0" 200 28 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0"
-"
以上是结束了!!接下来是要加强安全性,还是引入AP服务器,配置一个简单的应用程序来运行呢?
与第一次使用nginx相比,我更加苦恼在第一次使用Markdown上。