一、Windows下nginx的基本操作命令
首先进入nginx的安装目录:
1、启动 start nginx 或 nginx.exe
2、停止 nginx.exe -s stop 或 nginx.exe -s quit
3、重新载入配置 nginx.exe -s reload(每次修改配置文件都需要执行)
4、检查配置是否正确:nginx -t
5、查看任务进程:tasklist /fi "imagename eq nginx.exe"
二、负载均衡
1、增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器,也就是所说的负载均衡
2、客户端发送多个请求到服务器,服务器处理请求,有一些可能要与数据库进行交互,服务器处理完毕后,再将结果返回给客户端
三、动静分离
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力
四、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 {
listen 8089;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index one.html;
}
#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;
# }
#}
}
nginx配置文件有三部分组成
第一部分:全局块
从配置文件开始到events块之间的内容,主要会设置一些影响nginx服务器整体运行的配置指令,主要包括配置运行nginx服务器的用户(组)、允许生成的worker process 数,进程pid存放路径、日志存放路径和类型以及配置文件的引入等。
worker_processes 1;
这是nginx服务器并发处理服务的关键配置,worker_processes值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约,一般设置为cpu核心数
第二部分:events块
events{
worker_connections 1024;
}
events块涉及的主要影响nginx服务器与用户的网络连接,常用的设置包括是否开启对多work process下的网络连接进行序列化,是否允许同时接受多个网络连接,选取哪种事件驱动模型来处理连接请求,每个work process可以同时支持的最大连接数等。这部分的配置对nginx的性能影响较大,一般设置 cup数 * 1024 。
第三部分:http
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8089;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index one.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
这是nginx服务器配置中最频繁的部分,代理、缓存和日志定义等绝大部分功能和第三方模块的配置都在这里。
http块可以包括http全局块、service块
http全局块:配置的指令包括文件引入、MIME_TYPE定义、日志自定义、连接超时时间、单链接请求数上限等。
server块:这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。
每个http块可以包括多个server块,而每个server块就相当于一个虚拟主机。而每个server块也分为全局server块以及可以同时包含多个location块。
1、全局server块
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP配置。
2、location块
一个server块可以配置多个location块,这块的主要作用是基于nginx服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称(也可以是IP别名)之外的字符串(例如 前面的/uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。
3、proxy_pass
proxy_pass为代理转发模块,主要功能时把请求转发到其它服务上,当使用proxy_pass时,请求会在nginx中缓存,直到发送到后端的服务器上。
proxy_pass转发路径有三种:
假设用URL: http://location/web/test.html 进行访问
①proxy_pass后面的url加 / ,表示绝对根路径,不会代理location后的路径;
location /web/ {
proxy_pass http://192.168.1.198:8080/;
}
代理到URL:http://192.168.1.198:8080/test.html
②proxy_pass后面的url没有加 / ,则会把location后的路径代理进去;
location /web/{
proxy_pass http://192.168.1.198:8080;
}
代理到URL:http://192.168.1.198:8080/web/test.html
③proxy_pass后面的url加其他路径
location /web/{
proxy_pass http://192.168.1.198:8080/aaa/;
}
代理到URL:http://192.168.1.198:8080/aaa/test.html
④相对于第三种,最后少一个 /
location /web/{
proxy_pass http://192.168.1.198:8080/aaa;
}
代理到URL:http://192.168.1.198:8080/aaatest.html
4、nginx location匹配顺序
①“=”开头标识精确匹配
②“^~”开头表示uri以某个常规字符串开头,不是正则匹配
③“~”开头表示区分大小写的正则匹配
④“~*”开头表示不区分大小写的正则匹配
⑤“/”通用匹配,如果没有其它匹配,任何请求都会匹配到
1、location =/{
#精确匹配 / ,主机名后面不能带任何字符串
}
2、location / {
#因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
#但是正则和最长字符串会优先匹配
}
3、location /documents/{
#匹配任何以 /documents/开头的地址,匹配符合以后,还要继续往下搜索
#只有后面的正则表达式没有匹配到时,这一条才会采用这一条
}
4、location ~/documents/Abs{
#匹配任何以 /documents/开头的地址,匹配符合以后,还有继续往下搜索
#只有后面的正则表达式没有匹配到时,这一条才会采用这一条
}
5、location ^~/images/{
#匹配任何以 /images/开头的地址,匹配符合以后,停止往下搜索正则,采用这一条
}
6、location ~* \.(gif|jpg|jpeg)${
#匹配所有以 gif,jpg或jpeg结尾的请求
#然而,所有请求 /images/下的图片会被第五条处理,因为^~到达不了这一条正则
}
7、location /images/{
#字符匹配 /images/,继续往下会发现^~存在
}
8、location /images/abc{
#最长字符匹配到 /images/abc,继续往下会发现^~存在
}
9、location ~ /images/abc/{
#只有去掉第五条才有效:先最长匹配第八条开头的地址,继续往下搜索,匹配到这一条正则采用
}
优先级
(location ‘=’)> (location '完整路径') > (location '^~'路径 ) > (location '~','~*' 从上向下正则顺序,匹配在最后一条终止) > (location 部分起始路径) > ('/')
参考文章
https://blog.csdn.net/qq_40036754/article/details/102463099