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

Nginx搭建反向代理服务器

  • 1.实验物料
  • 2.搭建过程
    • 1.搭建静态资源Web服务器
    • 2.搭建反向代理服务器
    • 3.启动
    • 4.验证

1.实验物料

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

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

如下图所示:
CentOs-7下Nginx搭建反向代理服务器_第1张图片

2.搭建过程

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

关于静态资源Web服务器的搭建,我在上一篇博客《Nginx搭建静态资源Web服务器》已经做了详细说明,这里不再说明。
现在我就说一下台虚拟机如果启用多个nginx实例。
其实启用多个nginx的实例也很简单,只需要多复制几个nginx.conf的配置文件,做少许的修改就可以,然后我们在启动的时候指定不同的nginx.conf配置文件就可以了。我直接copy了两个配置,nginx-8081.conf和nginx-8082.conf放在了config目录下。
CentOs-7下Nginx搭建反向代理服务器_第2张图片
关于配置文件的具体内容,我直接贴出来:
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/,如下:
CentOs-7下Nginx搭建反向代理服务器_第3张图片
成功。

你可能感兴趣的:(软件安装,Nginx,nginx,负载均衡,linux,centos,运维)