CentOs-7下Nginx搭建反向代理服务器

1.实验物料

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

nginx-1.18.01个实例 端口80
nginx-1.18.02实例 端口分别为 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;
        }
    }
}

如果想配置缓存,可以采用下面的配置:

#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;
    proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=donkey_cache:20m max_size=18m inactive=60m
                     use_temp_path=on;    	
    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_cache donkey_cache;
           proxy_cache_key $host$uri$is_args$args;
           proxy_cache_valid 200 302 304 10m;
           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/,如下:
在这里插入图片描述
成功。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值