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

Nginx反向代理以及负载均衡配置

2017-05-15 15:17 302 查看
前提:最近在研究nginx的用法,在windows上小试了一下,由于windows下不支持nginx缓存配置,所以本文主要是讲nginx,以及反向代理与负载均衡。 【一、为什么要使用nginx】  要回答为什么要使用nginx,那就先说说nginx能做些什么。  首先,nginx能做反向代理,那么什么是反向代理呢,举个栗子,我想在本地使用 www.mickey.com 的域名去访问 www.taobao.com。那么这个时候我们就可以通过nginx去实现。  再者,nginx能实现负载均衡,什么是负载均衡呢?就是我的项目部署在不同的服务器上,但是通过统一的域名进入,nginx则对请求进行分发,减轻了服务器的压力。  在上面这两种情况下,nginx服务器的作用都只是作为分发服务器,真正的内容,我们可以放在其他的服务器上,这样来,还能起到一层安全隔壁的作用,nginx作为隔离层。  其次,nginx还能解决跨域的问题。 【二、、nginx安装】   在 http://nginx.org/ 下载对应版本的nginx   在 nginx 的目录下使用 start nginx 或者 双击 nginx.exe 打开nginx 【三、nginx配置属性说明】   
#全局设置
-1/var/log/nginx//var/run/nginx.pid;/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能worker_connections 1024/etc/nginx/mime.types;default_type  application/octet-stream;/var/log/nginx//O处理速度,降低系统的uptime.65"MSIE [1-6]\.(?!.*SV1)"4/etc/nginx/conf.d/*/etc/nginx/sites-enabled/*192.168.8.1:3128 weight=5192.168.8.2:80  weight=1192.168.8.3:80  weight=680/www.xx.com.access.log  main;//root;      #定义服务器的默认网站根目录位置/$fastcgi_script_name;include /etc/nginx/fastcgi_params;500 502 503 504 /50x.html;location = /50x.html {root   /root;~ ^/(images|javascript|js|css|flash|media|static)//var/www/virtual/~/root;fastcgi_pass 127.0.0.1:9000/home/www/www$fastcgi_script_name;/NginxStatus {"NginxStatus"/htpasswd;~ /\.ht {168.880192.168.8~ .*/root;#定义服务器的默认网站根目录位置-Forwarded--Real--Forwarded-9090904*2
【四、nginx反向代理】   本地起两个项目,源码在此。   分别在这两个文件夹下面运行    
npm installnode server.js
在浏览器输入本机ip:4789本机ip:5789可以访问到这两个页面接着我们想使用test.nginx.com访问到 页面5789test.nginx.com/bug 访问到页面5789则我们首先需要配置hostswin 下hosts 的地址为 C:\Windows\System32\drivers\etc我们需要在hosts文件里面添加如下配置
172.18.144.23 test.nginx.com
然后在 nginx 的 http 模块上添加一个 server
server {listen       80;server_name  test.nginx.com;location / {proxy_pass   http://172.18.144.23:4789/;        }location /buy {proxy_pass   http://172.18.144.23:5789/;        }error_page   500 502 503 504  /50x.html;location = /50x.html {            root   html;}}
然后重启nginx在浏览器输入 test.nginx.com在浏览器输入 test.nginx.com/bug反向代理就这样子啦。 【五、nginx负载均衡】 在nginx中配置http首先配置负载均衡的服务在http模块中添加如下配置
upstream webservers {server 172.18.144.23:4789 weight=10;server 172.18.144.23:5789 weight=10;}
把server改为
server {listen       80;server_name  test.nginx.com;location / {proxy_pass   http://webservers;        }location /buy {proxy_pass   http://172.18.144.23:5789/;        }error_page   500 502 503 504  /50x.html;location = /50x.html {            root   html;}}
在浏览器输入 test.nginx.com,刷新,我们可以看到两种页面,说明nginx已经把我们的请求分发到不同的地方去了。A
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  服务器 windows start