Nginx是一款轻量级的web服务器/反向代理服务器/及电子邮件(IMAP/POP3)代理服务器高性能web和反向代理服务器。
Nginx的三大功能:
一、服务器
nginx可以作为静态页面的服务器,在前后端分离开发中比较常见,如:前台静态资源部署在nginx上,后台服务部署在tomcat服务器中
二、虚拟主机
虚拟主机通过子域名可以实现多级域名,只需要在dns服务器上注册一个域名,通过nginx实现了多个域名:
如:注册 lishaojun.com 域名,可以在nginx中配置 aaa.lishaojun.com bbb.lishaojun.com 的映射到不同的资源(静态资源或者服务器)
三、反向代理
所谓的反向代理就是代理服务器,而不是客户端。通过反向代理可以实现服务器负载均衡的功能,负载均衡很好理解,在虚拟主机映射到服务器时,每个配置的域名(虚拟主机)可以配置多个服务器,而且可以配置权重,nginx自动选择合适的服务器转发请求。
总结:nginx可以直接作为网页服务器,或者间接代理服务器,并且可以实现域名虚拟化
Nginx的作为web服务的优势;
1,高并发连接:一台nginx服务器能够支撑5万并发连接在生产环境中跑到2~3万并发连接数
2,内存消耗少:在3万并发连接下,并开启的10个Nginx进程才消耗150M内存
3,配置文件简单,轻量级。
4,支持rewrite重写规则:能够根据域名,url的不同将http请求分到不同的后端服务器群组。
5,内置的健康检查功能:如果Nginx proxy后端的某台web服务器岩机了,不会影响前端访问。
Nginx的配置文件介绍:
Nginx的配置文件分为5块内容:
1,全局块:该部分配置主要影响Nginx全局,包括下面几个部分:
#配置运行Nginx服务器用户(组)
#worker process 数
#nginx进程的PID存放路径
#配置文件的引入
2,events块:该部分配置主要影响Nginx服务器与用户的网络连接:
#设置网络连接的序列化
#是否允许同时接受多个网络连接
#事件驱动模式模型的选择
#最大连接数的配置
3,http块:
#定义MIMI-type
#自定义服务日志
#允许sendfile方式传输文件
#连接超时时间
#单连接请求数上限
4,server块:
#location配置
#请求根目录配置
#更改location的URL
#网站默认首页配置
> #user nobody; 这表示以那个用户运行nginx
> worker_processes 4; 这表示开启几个worker线程,尽量比cpu的核数小一点,提高cpu缓存命中率
> worker_cpu_affinity 0001 0010 0100 1000; 这表示将四个worker线程绑定在特定的cpu上
> worker_rlimit_nofile 65535; 这表示所有worker进程能够打开的最大文件句柄数。
> #error_log logs/error.log;
> #error_log logs/error.log notice;
> #error_log logs/error.log info;
>
> #pid logs/nginx.pid;
>
>
> events {
> worker_connections 20480; } 这表示单个worker进程能够处理的最大并发连接数。
>
>
> 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; 是否开启压缩功能,最好开启,能够提升带宽。
> #gzip_comp_level 1; 这表示开启的压缩级别,最好调6或9
> include server.conf; include file ;文件的有引入,在这里我把nginx配置文件分成两个文件。另个文件为server.conf
> }
server.conf文件
server {
listen 80; 默认监听的80端口
server_name localhost; 域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html; 这是根目录
index index.html index.htm;
}
location /images { 定义多个location实现访问同一主机的不同URL
root "/www/80/";
if ($http_user_agent ~* Chrome) {
rewrite ^(.*\.html)$ /image/$1 break;
}
}
#error_page 404 /404.html; 定义错误页面信息。
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 8080; 基于8080的端口访问。
server_name www.node1.com;
location / {
root "/yan/8080/";
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl; 定义https访问路径,实现加密访问
# server_name localhost;
# ssl_certificate cert.pem; 申请的证书名称
# ssl_certificate_key cert.key; 证书的私钥
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}