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和tengine的tcp反向代理,负载均衡 安装和配置

    先下载nginx_tcp_proxy_module模块. wget https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master ...

  2. Nginx 反向代理 负载均衡 虚拟主机配置

    Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代理服务器的作用,区分正向代理和反向代理的区别,搭建使用Nginx反向搭理和负载均衡,了解Nginx常 ...

  3. 【转】Nginx 反向代理 负载均衡 虚拟主机配置

    原文:http://www.cnblogs.com/itdragon/p/8059000.html Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代 ...

  4. nginx负载均衡之入门配置

    先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况.那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上 ...

  5. 分布式系统的负载均衡以及ngnix负载均衡的五种策略

    一般而言,有以下几种常见的负载均衡策略: 一.轮询. 特点:给每个请求标记一个序号,然后将请求依次派发到服务器节点中,适用于集群中各个节点提供服务能力等同且无状态的场景. 缺点:该策略将节点视为等同, ...

  6. Nginx记录-nginx 负载均衡5种配置方式(转载)

    nginx 负载均衡5种配置方式 1.轮询(默认)   每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除.  2.weight 指定轮询几率,weight和访问比率成 ...

  7. nginx 负载均衡5种配置方式

    nginx 负载均衡5种配置方式 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight 指定轮询几率,weight和访问比率成正比, ...

  8. 服务器配置,负载均衡时需配置MachineKey

    服务器配置,负载均衡时需配置MachineKey https://blog.csdn.net/liuqiao0327/article/details/54018922 Asp.Net应用程序中为什么要 ...

  9. nginx之 nginx-1.9.7 + tomcat-8.5.15 反向代理+应用负载均衡 安装配置

    环境说明:nginx 反向代理服务器 ip 为: 10.219.24.26tomcat1 应用服务器 ip 为: 10.219.24.21tomcat3 应用服务器 ip 为: 10.219.24.2 ...

随机推荐

  1. spring boot整合mybatis方式一

    方式一: 导入maven依赖: <!--web依赖配置--> <dependency> <groupId>org.springframework.boot</ ...

  2. Web前端 前端工程师首选的几款编辑器/IDE以及Markdown的编辑器、语法

    前端工程师常使用的编辑器/IDE 本地在线工具 webstrom 推荐指数 ***** vs code 推荐指数 **** atom 推荐指数 **** subline-text 推荐指数 **** ...

  3. Linux 桌面玩家指南:18. 使用 Docker 隔离自己的开发环境和部署环境

    特别说明:要在我的随笔后写评论的小伙伴们请注意了,我的博客开启了 MathJax 数学公式支持,MathJax 使用$标记数学公式的开始和结束.如果某条评论中出现了两个$,MathJax 会将两个$之 ...

  4. 构造方法、This关键字、静态与封装的特性与作用

    1.构造方法 构造方法是一种特殊的方法,专门用于构造/实例化对象. 构造方法根据是否有参数分为无参构造方法和有参构造方法. 1.1无参构造方法 无参构造方法就是构造方法没有任何参数.无参构造方法在创建 ...

  5. 【重学计算机】机组D7章:总线

    1. 系统总线的特性及应用 总线概念:将计算机系统中各部件连接起来 总线分类:(外部/内部,系统/非系统,串行/并行,同步/异步...) 按用途分类: 存储总线:cpu与存储器 系统总线:连接存储总线 ...

  6. springboot~mockMvc和asciidoctor生成基于TDD的API文档

    API文档是前端与后端快速开发,减少沟通成本的必要条件,有一份完善的文档是很必要的,由通过测试来生成文档的好处就是:测试数据有了,测试返回结果有了,而且可以对这些字段进行说明,很清晰,在springb ...

  7. .netcore 模块积累

    最全的 demo https://github.com/XiaoFaye/netcore-samples http://files.cnblogs.com/files/kellynic/practic ...

  8. 机器学习之支持向量机—SVM原理代码实现

    支持向量机—SVM原理代码实现 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/9596898.html 1. 解决 ...

  9. Windows Server 2012 R2安装SqlServer 2016

    1.系统安装 微软操作系统 Windows Server 2012 R2 官方原版镜像 Windows Server 2012 R2 是由微软公司(Microsoft)设计开发的新一代的服务器专属操作 ...

  10. 在嵌入式设备中使用 JavaScript 的前景

    by Conmajia PC上的JavaScript已经发展到ECMAScript 6(ES6),马上ES10都快出来了(虽然还是草案),但是硬件上的JS却很少听说.这几年手持设备/可穿戴设备的发展非 ...