Nginx 配置
Nginx常用命令
tasklist /fi "imagename eq nginx.exe" 查看所有nginx进程
taskkill /fi "imagename eq nginx.EXE" /f 杀死所有nginx进程
nginx -t -c conf/nginx.conf 检查配置文件有无错误
nginx -s reload 重新启动nginx 不影响业务
nginx -s stop 快读停止nginx
start nginx 启动nginx
Nginx配置
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errors on;
server_names_hash_bucket_size 64;
#开启ip强制跳转 域名访问
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
rewrite ^(.*) https://www.xxx.shop$1 permanent;
}
# 允许通过域名访问-自动跳转到https
server {
listen 80;
server_name www.xxx.shop xxx.shop;
rewrite ^(.*) https://www.xxx.shop$1 permanent;
}
#https 监听443端口
server {
listen 443 ssl;
#listen 80;
server_name www.xxx.shop;
ssl_certificate cert/3825478_www.xxx.shop.pem;
ssl_certificate_key cert/3825478_www.xxx.shop.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#error_page 304 https://www.xxx.shop;
location / {
proxy_pass http://127.0.0.1:6004;
}
}
# 允许通过域名访问-自动跳转到https
server {
listen 80;
server_name media-cdn.xxx.shop;
rewrite ^(.*) https://media-cdn.xxx.shop$1 permanent;
}
#阿里云OSS 内部存储桶 反向代理
server {
listen 443 ssl;
#listen 80;
server_name media-cdn.xxx.shop;
ssl_certificate cert/4387813_media-cdn.xxx.shop.pem;
ssl_certificate_key cert/4387813_media-cdn.xxx.shop.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location / {
#proxy_pass http://127.0.0.1:6004;
expires 3d; #缓存3天
rewrite /(.+)$ /$1 break; #用rewrite进行url重写,使用户访问的url后缀,也传递到OSS上。
proxy_pass http://xxxx.xx-cn-shanghai-internal.aliyuncs.com; #阿里云OSS ecs内网访问地址
proxy_intercept_errors on; #必须传入,反向代理应用程序返回 错误
}
error_page 404 403 = /404.html; #定义404页面
#location = /404.html {
# 放错误页面的目录路径。
# root html;
#}
location ~* \.(gif|jpg|jpeg|png|css|js|ico|html)$ { #定义放置错误页面的路径
root html;
}
}
server{
listen 80;
server_name wechart.xxx.shop;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:6004;
}
}
server {
listen 80;
server_name lanyun.xxx.shop;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:6006;
}
}
server {
listen 80;
server_name laywechart.xxx.shop;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:6007;
}
}
server {
listen 80;
server_name *.xxx.shop;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:6006;
}
}
}
Nginx 正则匹配
^~ 标识符后面跟一个字符串。Nginx将在这个字符串匹配后停止进行正则表达式的匹配(location指令中正则表达式的匹配的结果优先使用),如:location ^~ /images/,你希望对/images/这个目录进行一些特别的操作,如增加expires头,防盗链等,但是你又想把除了这个目录的图片外的所有图片只进行增加expires头的操作,这个操作可能会用到另外一个location,例如:location ~* .(gif|jpg|jpeg)KaTeX parse error: Can't use function '\.' in math mode at position 117: …匹配到location ~* \̲.̲(gif|jpg|jpeg)这个location中,这并不是你需要的结果,而增加了^~这个标识符后,它在匹配了/images/这个字符串后就停止搜索其它带正则的location。
= 表示精确的查找地址,如location = /它只会匹配uri为/的请求,如果请求为/index.html,将查找另外的location,而不会匹配这个,当然可以写两个location,location = /和location /,这样/index.html将匹配到后者,如果你的站点对/的请求量较大,可以使用这个方法来加快请求的响应速度。
@ 表示为一个location进行命名,即自定义一个location,这个location不能被外界所访问,只能用于Nginx产生的子请求,主要为error_page和try_files。
~ 为区分大小写的匹配。
~* 不区分大小写的匹配(匹配firefox的正则同时匹配FireFox)。
!~ 不匹配的
!~* 不匹配的
. 匹配除换行符以外的任意字符
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
- 重复零次或更多次
- 重复一次或更多次
? 重复零次或一次
{n} 重复n次
{n,} 重复n次或更多次
{n,m} 重复n到m次
*? 重复任意次,但尽可能少重复
+? 重复1次或更多次,但尽可能少重复
?? 重复0次或1次,但尽可能少重复
{n,m}? 重复n到m次,但尽可能少重复
{n,}? 重复n次以上,但尽可能少重复
\W 匹配任意不是字母,数字,下划线,汉字的字符
\S 匹配任意不是空白符的字符
\D 匹配任意非数字的字符
\B 匹配不是单词开头或结束的位置
[^x] 匹配除了x以外的任意字符
[^aeiou] 匹配除了aeiou这几个字母以外的任意字符
捕获 (exp) 匹配exp,并捕获文本到自动命名的组里
(?exp) 匹配exp,并捕获文本到名称为name的组里,也可以写成(?'name’exp)
(?:exp) 匹配exp,不捕获匹配的文本,也不给此分组分配组号
零宽断言 (?=exp) 匹配exp前面的位置
(?<=exp) 匹配exp后面的位置
(?!exp) 匹配后面跟的不是exp的位置
(? 注释 (?#comment) 这种类型的分组不对正则表达式的处理产生任何影响,用于提供注释让人阅读
Nginx 注册为系统服务
windw 下载地址:https://github.com/winsw/winsw/releases
windw 仓库地址: https://github.com/winsw/winsw
配置说明:https://github.com/winsw/winsw/blob/master/doc/installation.md
下载Windows Service Wrapper 放在 nginx安装目录
下载好的可以该名字 同时创建一个同样名称的xml配置文件
<service>
<id>nginx</id>
<name>nginx</name>
<description>nginx</description>
<env name="path" value="C:/nginx-1.19.2/nginx-1.19.2"/>
<executable>C:/nginx-1.19.2/nginx-1.19.2/nginx.exe</executable>
<logpath>C:/nginx-1.19.2/logs/</logpath> //注意 可以放在跟nginx logs同目录下
<logmode>roll</logmode>
</service>