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通过端口区分不同虚拟机

#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/access.log main;
sendfile on;#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}}}

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

添加虚拟主机:

#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/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}}server {listen 81;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html-81;index index.html index.htm;}}}

重新加载配置文件

/nginx -s reload

4.2通过域名区分虚拟主机

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

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

ngnix服务器地址 :ceshi1.com

ngnix 服务器地址 :ceshi2.com

ngnix配置文件

#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/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}}server {listen 81;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html-81;index index.html index.htm;}}server {listen 80;server_name ceshi1.com;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}}server {listen 80;server_name ceshi2.com;#charset koi8-r;#access_log logs/host.access.log main;location / {root html81;index index.html index.htm;}}}

5.ngnix反向代理

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

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

2、启动tomcat。

3、ngnix文件配置

upstream tomcat1 {server 192.168.80.129:8080;}server {listen 80;server_name ceshi1.com;#charset koi8-r;#access_log logs/host.access.log main;location / {proxy_pass http://tomcat1;index index.html index.htm;}}upstream tomcat2 {server 192.168.80.129:8081;}server {listen 80;server_name ceshi2.com;#charset koi8-r;#access_log logs/host.access.log main;location / {proxy_pass http://tomcat2;index index.html index.htm;}}

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;

}

Ngnix负载均衡安装及配置相关推荐

  1. Nginx负载均衡的详细配置及使用案例

    Nginx负载均衡的详细配置及使用案例详解 感谢看过这一些列博文和评论的小伙伴, 我把自己所看到的学到的拿到这里来分享是想和大家一起学习进步, 想听听园友给出的意见, 也是对自己学习过程的一个总结.  ...

  2. centos6.9负载均衡方案完整配置(lvs+keepalived+pxc+nfs+业务系统)

    前期准备: NFS服务器:计算机名nfsserver,IP地址192.168.1.103,用于存放业务系统的数据. node1:计算机名PXC01,IP地址192.168.1.105,安装pxc系统和 ...

  3. f5 web服务器 位置,简说(地址和服务器池)F5负载均衡的部分配置

    前面我们对F5负载均衡器的准备和安装过程做了归纳,以及对它的初始化和通用设置进行了完整的步骤分析。现在,就来对后边的设置过程进行讲述。想要做好均衡工作,地址的分配是重点,这之中我们的IP分配设置,以及 ...

  4. ngnix 负载均衡原理

    ngnix 负载均衡原理

  5. f5负载均衡配置文件服务器,f5 负载均衡 dns 服务器 配置

    f5 负载均衡 dns 服务器 配置 内容精选 换一换 查询负载均衡器状态树.可通过该接口查询负载均衡器关联的监听器.后端云服务器组.后端云服务器.健康检查.转发策略.转发规则的主要信息,了解负载均衡 ...

  6. nginx负载均衡与日志配置

    nginx负载均衡与日志配置 1.设置nginx负载均衡 1.1 找到nginx.conf文件,并进行配置 2.设置nginx日志 2.1.找到nginx.conf文件,配置日志nginx日志 2.查 ...

  7. nginx+apache实现负载均衡+动静分离配置(编译安装)

    一.编译安装nginx cd /usr/local/src wget http://nginx.org/download/nginx-1.6.3.tar.gz tar -zxvf nginx-1.6. ...

  8. Nginx安装/负载均衡/反向代理配置与调优

    [Nginx安装] Linux下直接使用包管理安装 sudo apt-get install nginx 使用whereis命令查看安装位置 whereis nginx #sbin下代表nginx可执 ...

  9. Linux Centos7 Nginx的安装与配置、反向代理、负载均衡、https配置

    Nginx的安装 1.nginx安装包下载地址 http://nginx.org/en/download.html 2.把nginx安装包上传到Linux系统上 2.1 Xhell 自带上传工具. 2 ...

最新文章

  1. R语言可视化包ggplot2绘制线性回归模型曲线实战( Linear Regression Line)
  2. ecu根据什么信号对点火提前角_关于ECU的那点事
  3. 前端常见的加密算法介绍
  4. 关于异常:HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection的解决办法
  5. python requests 代理ip_python requests 测试代理ip
  6. C++自由存储空间:new
  7. java实时读取文件内容,java实时读取和写入文件
  8. Python+django网页设计入门(12):使用Bootstrap和jQuery
  9. 落实业务服务管理从基础设施管理做起
  10. JaxWsProxyFactoryBean调用WebService实例
  11. 天神娱乐实控人朱晔宣布离职:暂时的离开是为更好相见
  12. CSS进阶班笔记(五)
  13. 无法在路径“C:\WINDOWS\TEMP\”中创建临时文件: 拒绝访问。
  14. “武汉城市之根”发声: 又见宝藏系列数字藏品独家发行
  15. DDR和MIG使用小结
  16. 在python中如何读取批量图片_Python批量处理图片
  17. 室内空间摄影后期处理_摄影和录像中的后期制作或后期处理是什么?
  18. 网站遭到恶意攻击有什么危害
  19. 推荐5款经过时间验证的神级软件
  20. WAdmin 开源啦!!!

热门文章

  1. excel vba基础入门_[VBA][基础入门] 第3讲 认识VBA IDE(集成开发环境)
  2. android查询竞价处理,公平可靠的竞价方式,应对越来越高的流量获取成本,如何解决推广费用过高的问题可能是...
  3. stm32单片机屏幕一直闪_STM32F407[3] 闪烁LED
  4. 单靠MySQL进了字节,高端玩法才是王道!
  5. BAT 招聘岗位 100%都考的知识,你精通了吗?
  6. mysql 查看innodb版本,mysql中查看innodb版本的方法
  7. php 刷新iframe,js刷新iframe
  8. java arp 攻击_基于Jpcap的Java ARP断网攻击
  9. Rman duplicate数据库复制(单系统)
  10. 即插即用 | S-FPN全新的金字塔网络,更适合轻量化模型的FPN