一、Nginx安装及命令
1、docker安装
-
本文使用Docker进行安装Nginx
-
先将Nginx配置文件复制出来:
- 需要先创建文件夹conf 、 log
- docker cp nginx: 配置文件 ./
docker run -d --name nginx -p 80:80 -v $PWD/nginx.conf:/etc/nginx/nginx.conf -v /home/docker-nginx/conf:/etc/nginx/conf.d -v /home/docker-nginx/log/:/var/log/nginx c919045c4c2
2、安装nginx.tar
-
教程百度
-
命令
- 启动命令——在/usr/local/nginx/sbin目录下执行 ./nginx
- 关闭命令——在/usr/local/nginx/sbin目录下执行 ./nginx -sstop
- 重新加载命令——在/usr/local/nginx/sbin目录下执行 ./nginx -sreload
二、Nginx配置文件
nginx.conf 配置文件分为三部分:
1、第一部分:全局块
worker_processes auto;
-
这是Nginx服务器并发处理服务的关键配置
-
worker_processes值越大
-
可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约
2、第二部分:events块
events {
worker_connections 1024;
}
-
events块涉及的指令主要影响Nginx服务器与用户的网络连接
-
常用的设置包括是否开启对多work process下的网络连接进行序列化
-
是否允许同时接收多个网络连接
-
选取哪种事件驱动模型来处理连接请求
-
每个wordprocess可以同时支持的最大连接数等
上述例子就表示每个work process支持的最大连接数为1024
3、第三部分:http块
docker版nginx配置文件分两部分nginx.conf和default.conf
#------nginx.conf
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
#------default .conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/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 /usr/share/nginx/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;
#}
}
-
Nginx服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置
-
http块也可以包括http全局块、server 块。
-
一个http块可以包括多个server块,而每个server块就相当于一个虚拟主机。
-
每个server块也分为全局server块,以及可以同时包含多个locaton块。
a、全局server 块
- 最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP配置。
b、location 块
-
一个server块可以配置多个location块。
-
主要作用是基于Nginx服务器接收到的请求字符串(例如server_name/uri-string)。
-
对虚拟主机名称(也可以是IP别名)之外的字符串(例如前面的/uri-string)进行匹配,对特定的请求进行处理
-
地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。
c、location指令说明
该指令用于匹配URL
-
= :用于不含正则表达式的uri 前,要求请求字符串与uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
-
~:用于表示uri 包含正则表达式,并且区分大小写。
-
~:用于表示uri 包含正则表达式,并且不区分大小写。
-
^~:用于不含正则表达式的uri 前,要求Nginx 服务器找到标识uri和请求字符串匹配度最高的location 后,立即使用此location处理请求,而不再使用location块中的正则uri 和请求字符串做匹配。
注意:如果uri 包含正则表达式,则必须要有~ 或者~ 标识。
三、nginx配置实例
1、反向代理实例一
使用nginx反向代理,访问www.123.com 直接跳转到127.0.0.1:8080
server {
listen 80;
server_name www.123.com;
location / {
proxy_pass http://localhost:8080;
index index.html index.htm index.jsp
}
2、反向代理实例二
实现效果:使用nginx反向代理,根据访问的路径跳转到不同端口的服务中。
nginx监听端口为9001
访问http://127.0.0.1:9001/edu/直接跳转到127.0.0.1:8081
访问http://127.0.0.1:9001/vod/直接跳转到127.0.0.1:8082
server {
listen 80;
server_name localhost;
location ~ /edu/{
proxy_pass http://localhost:8001;
}
location ~ /vod/{
proxy_pass http://localhost:8002;
}
}
四、负载均衡
1、配置
http{
.....
upstream myserver{
ip_hash;
servere 115.28.52.63:8080 weight=1;
servere 115.28.52.63:8180 weight=1;
}
.....
server{
location /{
......
proxy_pass http://myserver;
proxy_connect_timeout 10;
}
......
}
}
2、Nginx负载均衡策略
-
轮询(默认)。每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
-
weight。weight代表权,重默认为1,权重越高被分配的客户端越多指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream myserver{ servere 115.28.52.63:8080 weight=10; servere 115.28.52.63:8180 weight=10; }
-
ip_hash。每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题.
upstream myserver{ ip_hash; servere 115.28.52.63:8080 weight=1; servere 115.28.52.63:8180 weight=1; }
-
fair(第三方)。按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream myserver{ servere 115.28.52.63:8080 ; servere 115.28.52.63:8180 ; fair; }
五、nginx动静分离
严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat处理动态页面。
通过location 指定不同的后缀名实现不同的请求转发。
1、配置文件
server {
listen 80;
server_name localhost;
location /www/{
root /data/;
index index.html index.htm;
}
location /image/{
root /data/;
autoindex on;
}
}
六、nginx原理与优化参数配置
1、master-workers机制
-
对于每个worker进程来说,独立的进程,不需要加锁。
-
一个进程退出后,其它进程还在工作,服务不会中断,master进程则很快启动新的worker进程
a、设置worker数量
每个worker的线程可以把一个cpu的性能发挥到极致。所以worker数和服务器的cpu数相等是最为适宜的。
#设置worker数量。
worker_processes 4
#work绑定 cpu(4 work绑定4cpu)。
worker_cpu_affinity 0001 0010 0100 1000
#work绑定cpu (4 work绑定8cpu中的4个)。
worker_cpu_affinity 0000001 00000010 00000100 00001000
b、连接数worker_connection
这个值是表示每个worker进程所能建立连接的最大值。
nginx能建立的最大连接数(最 大 并 发 数 量) = worker_connections * worker_processes。
七、nginx搭建高可用集群
1、Keepalived+Nginx高可用集群(主从模式)
2、Keepalived+Nginx高可用集群(双主模式)
百度教程...
评论区