您的位置:首页 > 运维架构 > 反向代理

Nginx搭建反向代理服务器

2020-07-26 20:31 1501 查看

Nginx搭建反向代理服务器

1.实验物料

3个Nginx的实例,其中一个作为反向代理服务器,另外两个作为静态资源Web服务器,还有静态资源

nginx-1.18.0 1个实例 端口80
nginx-1.18.0 2实例 端口分别为 8081 8082
静态HTML

如下图所示:

2.搭建过程

1.搭建静态资源Web服务器

关于静态资源Web服务器的搭建,我在上一篇博客《Nginx搭建静态资源Web服务器》已经做了详细说明,这里不再说明。
现在我就说一下台虚拟机如果启用多个nginx实例。
其实启用多个nginx的实例也很简单,只需要多复制几个nginx.conf的配置文件,做少许的修改就可以,然后我们在启动的时候指定不同的nginx.conf配置文件就可以了。我直接copy了两个配置,nginx-8081.conf和nginx-8082.conf放在了config目录下。

关于配置文件的具体内容,我直接贴出来:
nginx-8081.conf的配置:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
worker_connections  1024;
}

http {
include       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  logs/8081.log  main;
sendfile        on;
#tcp_nopush     on;
#keepalive_timeout  0;
keepalive_timeout  65;
gzip  on;
gzip_min_length 1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml
text/javascript application/x-httpd-php image/jpeg image/gif image/png;
server {
listen       127.0.0.1:8081;
#server_name  localhost;
#charset koi8-r;
access_log  logs/8081.access.log  main;
location / {
#root   html;
#index  index.html index.htm;
alias /var/html/dreamRoad/;
autoindex on;
set $limit_rate 2m;
}
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}
}

nginx-8082.conf的配置:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
worker_connections  1024;
}

http {
include       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  logs/8082.log  main;
sendfile        on;
#tcp_nopush     on;
#keepalive_timeout  0;
keepalive_timeout  65;
gzip  on;
gzip_min_length 1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml
text/javascript application/x-httpd-php image/jpeg image/gif image/png;
server {
listen       127.0.0.1:8082;
#server_name  localhost;
#charset koi8-r;
access_log  logs/8082.access.log  main;
location / {
#root   html;
#index  index.html index.htm;
alias /var/html/dreamRoad/;
autoindex on;
set $limit_rate 2m;
}
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}
}

2.搭建反向代理服务器

配置nginx.conf,配置内容如下:

#user  nobody;
worker_processes  1;
events {
worker_connections  1024;
}
http {
include       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  logs/access.log  main;
sendfile        on;
keepalive_timeout  65;
gzip  off;
gzip_min_length 1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml
text/javascript application/x-httpd-php image/jpeg image/gif image/png;
upstream dream {
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen       80;
server_name  dream.donkey.com;
access_log  logs/access.log  main;
location / {
proxy_pass       http://dream;
proxy_set_header Host      $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}
}

3.启动

  1. 启动资源服务器
./sbin/nginx -c /usr/local/nginx/conf/nginx-8081.conf
./sbin/nginx -c /usr/local/nginx/conf/nginx-8082.conf
  1. 启动代理服务器
./sbin/nginx

4.验证

利用浏览器访问http://dream.donkey.com/,如下:

成功。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: