nginx是一款高性能的http服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。官方测试nginx可以支撑5w并发链接。而且cup、内存等资源消耗却很是低,运行很是稳定。html
1.http服务器。nginx是一个http服务,能够独立提供http服务。能够作网页静态服务器。
2.虚拟主机。能够实如今一台服务器中虚拟出多个网站。相似于购买的虚拟主机。
3.反向代理,负载均衡。当网站的访问量达到必定的程度后,单台服务器不能知足用户的请求时,须要多台服务器集群可使用nginx作反向代理。而且多台服务器能够平均分担负载,不会由于某台服务器负载高宕(dang)机而某台服务器闲置的状况。linux
官网下载nginx安装包(http://nginx.org/)nginx
安装gcc的环境。c++
yum install gcc-c++
安装PCRE。web
(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,因此须要在linux上安装pcre库。注:pcre-devel是使用pcre开发的一个二次开发库。nginx也须要此库。正则表达式
yum install -y pcre pcre-devel
安装zlib。算法
zlib库提供了不少种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,因此须要在linux上安装zlib库。浏览器
yum install -y zlib zlib-devel
安装openssl。tomcat
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、经常使用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不只支持http协议,还支持https(即在ssl协议上传输http),因此须要在linux安装openssl库。安全
yum install -y openssl openssl-devel
把nginx的源码包上传到linux系统
解压缩
tar zxf nginx-1.8.0.tar.gz
使用configure命令建立一makeFile文件。
./configure \ --prefix=/usr/softwear/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
注意:启动nginx以前,上边将临时文件目录指定为/var/temp/nginx,须要在/var下建立temp及nginx目录
mkdir /var/temp/nginx/client -p
make
make install
经常使用命令
进入sbin目录 启动nginx: ./nginx 关闭nginx: ./nginx -s stop ./nginx -s quit (推荐使用) 刷新配置文件: ./nginx -s reload 检查配置文件: ./nginx -t
访问nginx:
http://localhost:80
Nginx的配置文件: 安装目录/conf/nginx.conf
#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 { #监听80端口 listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } } server { #监听81端口 listen 81; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-81; index index.html index.htm; } } }
#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 www.pyfysf.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-pyfysf; index index.html index.htm; } } server { listen 80; server_name www.pyfysf888.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-pyfysf888; index index.html index.htm; } } }
上述配置文件就能够实现经过配置不一样的域名进行不一样的跳转内容。(当前仅仅是静态资源访问)
经过浏览器访问:http://www.pyfysf.com 和 http://www.pyfysf888.com 两个域名同时绑定到一个ip地址(本地测试能够经过修改host文件)
反向代理服务器决定哪台服务器提供服务。
返回代理服务器不提供服务器。也是请求的转发。
upstream tomcat1 { server 192.168.25.148:8080; } server { listen 80; server_name www.pyfysf.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.25.148:8081; } server { listen 80; server_name www.pyfysf888.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://tomcat2; index index.html index.htm; } }
若是一个服务由多条服务器提供,须要把负载分配到不一样的服务器处理,须要负载均衡。
upstream tomcat2 { server 192.168.25.148:8081; server 192.168.25.148:8082; }
能够根据服务器的实际状况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1
upstream tomcat2 { server 192.168.25.148:8081; server 192.168.25.148:8082 weight=2; }