什么是Nginx?
Nginx是十分轻量级的HTTP服务器,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器
Nginx的优点
Nginx凭借其稳定性、低资源消耗、简单配置和丰富的功能
1、作为Web服务器,Nginx处理静态文件、索引文件,自动索引的效率非常高
2、作为代理服务器,Nginx可以实现无缓存的反向代理加速,提高网站运行速度
3、作为负载均衡服务器,Nginx既可以在内部直接支持Rails和PHP,也可以支持HTTP代理服务器对外进行服务,同时还支持简单的容错和利用算法进行负载均衡
4、在性能方面,Nginx是专门为性能优化而开发的,实现上非常注重效率。它采用内核Poll模型,可以支持更多的并发连接,最大可以支持对5万个并发连接数的响应,而且只占用很低的内存资源
5、在稳定性方面,Nginx采取了分阶段资源分配技术,使得CPU与内存的占用率非常低。Nginx官方表示,Nginx保持1万个没有活动的连接,而这些连接只占用2.5MB内存,因此,类似DOS这样的攻击对Nginx来说基本上是没有任何作用的
6、在高可用性方面,Nginx支持热部署,启动速度特别迅速,因此可以在不间断服务的情况下,对软件版本或者配置进行升级,即使运行数月也无需重新启动,几乎可以做到7x24小时不间断地运行
-
正反代理
正向代理是一个位于客户端和原始服务器之间的服务器,为了从原始服务器取的内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端,客户端必须要进行一些特别的设置才能使用正向代理。
反向代理服务器:在服务器端接收客户端的请求,然后把请求分发给具体的服务器进行处理,然后再将服务器的响应结果反馈给客户端。Nginx就是其中的一种反向代理服务器软件。
-
负载均衡
单个服务器解决不了,增加服务器数量,然后将请求分发到各个服务器
上,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,
将负载分发到不同的服务器,也就是所说的负载均衡。
-
动静分离
加快网站网站的解析速度,可以把动态页面和静态页面由不同的服务器来
解析,加快解析速度。降低原来单个服务器的压力。
Ngingx常用命令
cd /xx/xx/nginx/sbin/
./nginx 启动
./nginx -s stop 停止
./nginx -s quit 安全退出
./nginx -s reload 重新加载配置文件
检查conf 文件是否有语法错误
Nginx 配置文件详解
##这个就是我们打交道最多的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;
#以上叫做全局配置#
#----------------------------------------------------------------------------#
####最大监听数 默认1024###
events {
worker_connections 1024;
}
#----------------------------------------------------------------------------#
#http相关配置#
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;
## 80就是http的默认请求端口 ##
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
#}
}
# 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;
# 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;
# }
#}
}
我们主要关注的其实也就是 http / https 相关配置
#http相关配置#
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
##负载均衡配置 wblx是自定义去写的 ##
upstream wblx {
# 服务器资源
server xxx.x.x.x:端口;
#例如#
server 127.0.0.1:8080;
server 127.0.0.2:8081;
#上面两个机器的配置 叫轮循 就是一个接着一个#
#-------------------------------#
#----这种叫权重 每3次请求 1次在0.1 2次在0.2-------#
server 127.0.0.1:8080 weight=1;
server 127.0.0.2:8081 weight=2;
#-------------------------------#
#备份和宕机#
server 127.0.0.1:8080 ;
server 127.0.0.2:8081 backup; #这台机器就是,备份 除非上面机器挂了在使用#
server 127.0.0.1:8080 ;
server 127.0.0.2:8081 down; #这台机器就是,宕机 不参与负载了#
}
###http 请求模式就是80端口 ###
server {
listen 80;
server_name localhost;
###http请求发起 路径为 / 就会走到这一步 ###
location / {
root html;
index index.html index.htm;
##配置代理 proxy_pass http://负载均衡配置名称##
proxy_pass http://wblx;
}
###我们可以配置 或者多加 例如 /admin 的请求走这里 ##
location /admin {
xxx;
xxx;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Nginx 部署静态网站
server {
listen 80;
server_name localhost;
###http请求发起 路径为 / 就会走到这一步 ###
location / {
# root 静态网站文件的Linux地址#
# index 静态页面(进入静态网站文件的Linux地址 去找index.html/index.htm)
root /opt/xxx/xxx/;
index index.html index.htm;
}
Nginx 实现静态代理
把静态资源 css js html等交给nginx处理 而不是tomcat处理
通过nginx.conf 配置文件中添加静态资源的location
默认所有80端口的 所有 .js .css .html的静态文件都去 /opt/static 里面去找
或者
Nginx 实现动静分离
##实现以下上面的模型##
1. 复制出来三个 conf 文件
配置端口80 81 82
2. 80设置成负责 负载均衡的 机器
在80 添加upstream
------这样静态资源就负载均衡走到 127.0.0.1:81 127.0.0.1:82 里面了
upstream static.wblx {
server 127.0.0.1:81;
server 127.0.0.1:82;
}
location ~ .*(css|js|img|images) {
proxy_pass http://static.wblx;
}
静态资源走到 127.0.0.1:81 127.0.0.1:82 nginx
而且这两个conf文件就是配置了