1.ngnix概念
Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。
2.ngnix应用场景
http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。
虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。
3.ngnix安装
3.1下载
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
./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;
}
评论(0)