What——什么是nginx

nginx是一款高性能的http服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。官方测试nginx能够支撑5w并发连接。并且cup、内存等资源消耗却非常低,运行非常稳定。

Function——功能

1.http服务器。nginx是一个http服务,可以独立提供http服务。可以做网页静态服务器。

2.虚拟主机。可以实现在一台服务器中虚拟出多个网站。类似于购买的虚拟主机。

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

How——如何使用

install——安装

官网下载nginx安装包(http://nginx.org/)

Linux系统安装之前需要环境
  1. 安装gcc的环境。

    yum install gcc-c++
  2. 安装PCRE。

    (Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

    yum install -y pcre pcre-devel
  3. 安装zlib。

    zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

    yum install -y zlib zlib-devel
  4. 安装openssl。

    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

    nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

    yum install -y openssl openssl-devel
安装步骤
  1. 把nginx的源码包上传到linux系统

  2. 解压缩

    tar zxf nginx-1.8.0.tar.gz

  3. 使用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
  1. make
  2. make install

常用命令

进入sbin目录
启动nginx:
./nginx 关闭nginx:
./nginx -s stop
./nginx -s quit (推荐使用)
刷新配置文件:
./nginx -s reload
检查配置文件:
./nginx -t

访问nginx:

http://localhost:80

config——配置

Nginx的配置文件:
安装目录/conf/nginx.conf
  1. 通过端口区分不同虚拟机(通过配置nginx监听不同的端口,从而实现不同端口下不同的跳转)

#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;
}
} }
  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 {
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.comhttp://www.pyfysf888.com 两个域名同时绑定到一个ip地址(本地测试可以通过修改host文件)

反向代理

  1. 正向代理

  1. 反向代理

反向代理服务器决定哪台服务器提供服务。

返回代理服务器不提供服务器。也是请求的转发。

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;
}

最新文章

  1. 豆瓣FM 歌词跟随插件
  2. jQuery的扩展与noConflict
  3. 如何编写Angular指令
  4. maven 构建spring ssh mybatis 配置
  5. 学习笔记_JDBC_1_Demo1_连接数据库的基本操作和步骤
  6. 使用宏定义来减少JNI的繁琐
  7. 《Linear Algebra and Its Applications》-chaper3-行列式-从一个逆矩阵算法证明引入的行列式
  8. 开源搜索引擎评估:lucene sphinx elasticsearch
  9. WeQuant交易策略—简单均线
  10. 201521123075 《Java程序设计》第7周学习总结
  11. CSS中的块级元素(block)与行内元素(inline)
  12. 为Ghost博客扩展代码高亮、数学公式、页面统计、评论
  13. C# 哈希表HashTable的简单使用
  14. Matlab 读取excel文件提示服务器出现意外情况或无法读取问题解决
  15. docker相关杂项
  16. 深入理解Java虚拟机&运行时数据区
  17. feignclient设置hystrix参数
  18. java回调方法之理解
  19. How to run WPF – XBAP as Full Trust Application
  20. django 连接mysql报错

热门文章

  1. Rainyday.js – Rendering Raindrops with JavaScript
  2. mysql自动安装教程说明
  3. linux下mysql定时备份
  4. KM算法 详解+模板
  5. 3013C语言_输入输出
  6. 《SpringMVC从入门到放肆》十五、SpringMVC之上传文件
  7. 程序代写, CS代写, 代码代写, CS编程代写, java代写, python代写, c++/c代写, R代写, 算法代写, web代写
  8. selenium工作原理详解
  9. 【练习题】proj1 判断二叉树子树和是否为指定的值
  10. JS处理时间戳、前台拿到日期时间戳,时间戳转日期格式