nginx做反向代理

反向代理概念:

最初是client访问server,但是使用代理服务器去代理server后,当client访问server时就不会直接访问server,而是先访问代理服务器,由代理服务器再去访问serevr。但是client并不知道自己访问的是一台代理服务器。这个代理服务器就是反向代理。

反向代理与正向代理的区别:

反向代理代理server,正向代理代理client。

使用nginx做反向代理:

nginx可实现url的转发,完成反向代理的功能。

举例说明:

我们要代理的服务器IP:PORT为2.2.2.2:80

nginx本机的IP:PORT为1.1.1.1:8011进行代理。

也就是说用户本来访问服务器要访问2.2.2.2:80,但经过我们代理后,用户访问1.1.1.1:8011就可以访问服务器。

下面开始说如何实现。

使用方法——建立用户:

useradd -s /bin/false -r -M -d /nonexistent www

使用方法——编译安装:

   ./configure \
   --user=www \
   --group=www \
   --prefix=/usr/local/nginx \
   --sbin-path=/usr/local/nginx/bin/nginx \
   --conf-path=/usr/local/nginx/conf/nginx.conf \
   --pid-path=/var/run/nginx.pid \
   --lock-path=/var/lock/nginx.lock \
   --error-log-path=/dev/null \
   --http-log-path=/dev/null \
   --with-threads \
   --with-file-aio \
   --with-http_secure_link_module \
   --with-http_realip_module \
   --with-http_sub_module \
   --with-http_gunzip_module \
   --with-http_slice_module \
   --with-http_ssl_module \   
   --with-http_stub_status_module \
   --without-http_uwsgi_module \
   --without-http_scgi_module \
   --without-http_autoindex_module \

使用方法——给文件夹授权:

chown -R www:www /usr/local/nginx

使用方法——修改配置文件:

修改/usr/local/nginx/nginx.conf如下
user nobody;
worker_processes 10;
thread_pool default threads=32 max_queue=65536;
worker_rlimit_nofile 65535;
events {
          use epoll;
          worker_connections  65535;
}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" "$gzip_ratio"';
          sendfile        on;
          #tcp_nopush     on;
          #keepalive_timeout  0;
          proxy_cache_path /tmp/proxycache levels=1:2 keys_zone=cache_one:10m inactive=24h max_size=5g;
          gzip  on;
          include test/server.conf;
}


创建/usr/local/nginx/conf/test/upteam.conf并写入下面内容
upstream test{
    server 2.2.2.2:80;
}

创建/usr/local/nginx/conf/test/server.conf并写入下面内容
server {
        listen       1.1.1.1:8011;
        server_name  localhost;
        location / {
            proxy_pass   http://test;
            index  index.html index.htm;
        }     
}
如此,nginx的反向代理就搭建完了。启动即可。在下一篇会讲解如何控制nginx的状态。


完!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值