Ngnix负载均衡安装及配置

前端之家收集整理的这篇文章主要介绍了Ngnix负载均衡安装及配置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.ngnix概念

Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试Nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。

2.ngnix应用场景

http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。

虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。

反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用Nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

3.ngnix安装

3.1下载

http://nginx.org/

3.2安装

1、安装gcc的环境。yum install gcc-c++

2、安装pcre库。yum install -y pcre pcre-devel

3、安装zlib库。yum install -y zlib zlib-devel

4、安装openssl库。yum install -y openssl openssl-devel

5、把Nginx的源码包上传到linux系统

6、解压缩

7、进入解压后的目录,使用configure命令创建一个makeFile文件

./configure \

--prefix=/usr/local/Nginx \

--pid-path=/var/run/Nginx/Nginx.pid \

--lock-path=/var/lock/Nginx.lock \

--error-log-path=/var/log/Nginx/error.log \

--http-log-path=/var/log/Nginx/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/Nginx/client \

--http-proxy-temp-path=/var/temp/Nginx/proxy \

--http-fastcgi-temp-path=/var/temp/Nginx/fastcgi \

--http-uwsgi-temp-path=/var/temp/Nginx/uwsgi \

--http-scgi-temp-path=/var/temp/Nginx/scgi

8、建立文件

mkdir /var/temp/Nginx/client –p

9、执行make命令 make

10、执行make install 命令 make install

11、安装完毕

3.3启动ngnix

1、进入ngnix的sbin目录

cd /usr/local/ngnix/sbin

2、执行命令

./Nginx

3、查看ngnix是否启动

ps –ef | grep ngnix

3.4关闭ngnix

第一种方式:./Nginx -s stop

第二种方式(推荐): ./Nginx -s quit

3.5重启ngnix

1.先关闭后启动。
2.刷新配置文件

./ngnix –s reload

3.6访问ngnix

访问本级ip即可,默认为80端口。需要关闭防火墙

关闭防火墙:chkconfig iptables off

4.配置虚拟主机

ngnix配置文件:/usr/local/Nginx/conf/Nginx.conf

4.1通过端口区分不同虚拟机

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/Nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
    #tcp_nopush on;
  18. #keepalive_timeout 0;
  19. keepalive_timeout 65;
  20. #gzip on;
  21. server {
  22. listen 80;
  23. server_name localhost;
  24. #charset koi8-r;
  25. #access_log logs/host.access.log main;
  26. location / {
  27. root html;
  28. index index.html index.htm;
  29. }
  30. }
  31. }

可以配置多个server,配置了多个虚拟主机。

添加虚拟主机:

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/Nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 80r;
  24. #access_log logs/host.access.log main;
  25. location / {
  26. root html;
  27. index index.html index.htm;
  28. }
  29. }
  30. server {
  31. listen 81 {
  32. root html-81;
  33. index index.html index.htm;
  34. }
  35. }
  36. }

重新加载配置文件

/Nginx -s reload

4.2通过域名区分虚拟主机

在本机host文件中,设置两个用于测试的域名

修改window的hosts文件:(C:\Windows\System32\drivers\etc)

ngnix服务器地址 :ceshi1.com

ngnix 服务器地址 :ceshi2.com

ngnix配置文件

  1. ;
  2. #error_log logs/error.log;
  3. #pid logs/Nginx.pid;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. #log_format main '$remote_addr - $remote_user [$time_local] "$request" ';
  11. #access_log logs/access.log main;
  12. sendfile on;
  13. #tcp_nopush on;
  14. #keepalive_timeout 0;
  15. #gzip on;
  16. server {
  17. listen 80;
  18. server_name localhost;
  19. #charset koi8-r;
  20. #access_log logs/host.access.log main;
  21. location /;
  22. index index.html index.htm;
  23. }
  24. }
  25. server {
  26. listen 80;
  27. server_name ceshi1.com;
  28. #charset koi8- {
  29. root html;
  30. index index.html index.htm;
  31. }
  32. }
  33. server {
  34. listen 80;
  35. server_name ceshi2.com;
  36. #charset koi8- {
  37. root html81;
  38. index index.html index.htm;
  39. }
  40. }
  41. }

5.ngnix反向代理

两个域名指向同一台Nginx服务器,用户访问不同的域名显示不同的网页内容

1、安装两个tomcat。分别运行在8080和8081。

2、启动tomcat。

3、ngnix文件配置

  1. upstream tomcat1 {
  2. server 192.168.80.129:8080;
  3. }
  4. server {
  5. listen 80 {
  6. proxy_pass http://tomcat1;
  7. index index.html index.htm;
  8. }
  9. }
  10. upstream tomcat2 {
  11. server 192.168.80.129:8081tomcat2;
  12. index index.html index.htm;
  13. }
  14. }

4、重新加载配置文件

6.ngnix负载均衡

如果一个服务由多条服务器提供,需要把负载分配到不同的服务器处理,需要负载均衡。

只需在upstream 内配置多个服务地址即可。

upstream tomcat2 {

    server 192.168.80.129:8081;

    server 192.168.80.130:8082;

}

可以根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1

upstream tomcat2 {

    server 192.168.80.129:8081;

    server 192.168.80.130:8082 weight=2;

}

猜你在找的Nginx相关文章