Windows版本
1.下载nginx
nginx常用命令
- 启动命令
start nginx- 快速停止
nginx -s stop- 有序停止
nginx -s quit- 重启命令
nginx -s reloadnginx配置文件
- 文件结构
... #全局块
events { #events块
...
}
http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块
}server块
- 设置根路径,默认在html下
server {
listen 80;
server_name localhost;
root ../../webapps;
}location块
- 执行优先级
- 完整路径:(location = /index.html)
- 前缀匹配:(location ^~ /static)
- 正则匹配:(location ~* .(jpg|jpeg)$)
- 部分前缀:(location /static)
- 通用匹配:(location /)
- 表达式
| 正则表达式语法 | 含义 | 示例 |
|---|---|---|
. | 匹配任意单个字符(除了换行符) | a.b 匹配 "acb", "abb", 但不匹配 "a\nb" |
* | 前一个字符出现0次或多次 | a.*b 匹配 "ab", "axxb", "acdb" 等 |
+ | 前一个字符出现1次或多次 | a+b 匹配 "ab", "aab", 但不匹配 "a" |
? | 前一个字符出现0次或1次 | colou?r 匹配 "color" 或 "colour" |
{n} | 前一个字符精确出现n次 | a{2}b 匹配 "aab" |
{n,} | 前一个字符至少出现n次 | a{2,} 匹配 "aa", "aaa", "aaaa" 等 |
{n,m} | 前一个字符出现n到m次 | a{2,4} 匹配 "aa", "aaa", "aaaa" |
^ | 行的开始 | ^abc 只匹配以 "abc" 开头的行 |
$ | 行的结束 | abc$ 只匹配以 "abc" 结尾的行 |
[] | 字符集,匹配括号内的任意一个字符 | [abc] 匹配 "a", "b", 或 "c" |
[^] | 负向字符集,匹配不在括号内的任意字符 | [^abc] 匹配除 "a", "b", "c" 之外的任何字符 |
- | 在字符集中表示范围 | [a-z] 匹配任何小写字母 |
() | 分组,用于捕获或分隔子表达式 | (ab)+ 匹配 "ab", "abab", "ababab" 等 |
| | 或运算符,匹配左边或右边的内容 | cat|dog 匹配 "cat" 或 "dog" |
- 通用匹配
location / {
root html;
index index.html index.htm;
}应用案例
应用一:在html文件夹下,匹配条件拼接到路径中
http://localhost:80/first文件位置:html/first/
location /first {
#第一个工程
root html;
index index.html;
}应用二:在html文件夹下,使用别名,访问最终路径
http://localhost:80/newname文件位置:html/first/
location /newname {
#重命名工程
alias html/first/; #路径结尾要加”/”
index index.html;
}应用三:父路径,使用../ 请求到nginx的父路径
http://localhost:80/outer/static文件位置:../../webapps/outer/static/first/
location /outer/static {
#外部路径
root ../../webapps;
index index.html;
}应用四:父路径,使用别名
http://localhost:80/fine-desktop文件位置:../../webapps/fine-desktop-1.0/static/
location /fine-desktop {
#fine-desktop
alias ../../webapps/fine-desktop-1.0/static/;
index index.html;
}应用五:绝对路径
http://localhost:80/static文件位置:F:/windows/webapps/fine-desktop-1.0/static/
location /static {
#static
root F:/windows/webapps;
index index.html;
}应用六:正则匹配
http://localhost:80/应用名称/static文件位置:../../webapps/应用名称/static/
location ~* /*/static {
#static
root ../../webapps;
index index.html;
}文件引用
- server块内引用location
在nginx安装路径下创建引入文件:conf/location/lib.conf
location /fine-service-1.0/static {
#static
root ../../lib;
index index.html;
}在nginx安装路径下创建引入文件:conf/location/webapps.conf
location /fine-folder-1.0/static {
#static
root ../../webapps;
index index.html;
}在nginx配置文件中include文件:conf/nginx.conf
- 访问lib配置:http://127.0.0.1:80/fine-service-1.0/static/
- 访问apps配置:http://127.0.0.1:80/fine-folder-1.0/static/
server {
listen 80;
server_name localhost;
# server块中引用配置文件
include location/*.conf;
}2.nginx集成
- 解压nginx,并将文件夹命名为:
nginx-1.24.0 - 将
nginx.exe程序重命名为fine-nginx.exe,后期在进程使用fine-nginx名称查询 - 创建
bin文件夹,并创建脚本文件:startup.bat、shutdown.bat、restart.bat、progress.bat、status.bat - 修改
nginx.conf配置文件 - 将
nginx-1.24.0集成到lib目录中 - 创建端口
port.conf和根路径root.conf配置文件 - 将网站图标
favicon.ico拷贝到html目录下
startup.bat:nginx启动脚本
@echo off
title "nginx 1.24.0"
setlocal
set "BIN_DIR=%~dp0"
cd %BIN_DIR%
cd ..
tasklist | find /i "fine-nginx.exe"
if errorlevel 1 (
fine-nginx
)
endlocalshutdown.bat:nginx有序关闭脚本
@echo off
title "nginx 1.24.0"
setlocal
set "BIN_DIR=%~dp0"
cd %BIN_DIR%
cd ..
fine-nginx -s quit
endlocalrestart.bat:nginx重启脚本
@echo off
title "nginx 1.24.0"
setlocal
set "BIN_DIR=%~dp0"
cd %BIN_DIR%
cd ..
fine-nginx -s reload
endlocalprogress.bat:nginx是否启动脚本
@echo off
setlocal enabledelayedexpansion
set "process_name=fine-nginx.exe"
for /f "tokens=1" %%a in ('tasklist /fi "imagename eq !process_name!" /nh') do (
if %%a equ !process_name! (
echo 1
) else (
echo 0
)
goto :break
)
:break
endlocalstatus.bat:nginx是否启动完成脚本
@echo off
setlocal enabledelayedexpansion
set "process_name=fine-nginx.exe"
for /f "tokens=1" %%a in ('tasklist /fi "imagename eq !process_name!" /nh') do (
if %%a equ !process_name! (
echo 1
) else (
echo 0
)
goto :break
)
:break
endlocal配置文件说明
nginx.conf:主要将端口listen和应用location交由外部配置文件配置。这样在工程中就可以直接修改程序端口和动态配置应用程序
port.conf:主要设置端口配置和根路径反向代理,因为反向代理可能涉及端口修改,所以该配置中也执行了root.conf中的功能
root.conf:目前该文件没有生效的location配置块,根路径配置交由端口配置port.conf管理,但当在系统运行过程中,会在改文件的相同路径下动态产生应用程序的location配置块,起到程序自动注册作用。
nginx.conf: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 80;
# listen [::]:80;
# server_name localhost;
include ../../../conf/port.conf;
include ../../../conf/location/*.conf;
#charset koi8-r;
#access_log logs/host.access.log main;
location = /favicon.ico {
root html;
log_not_found off;
access_log off;
}
#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;
# }
#}
}port.conf:端口配置文件
../../../conf/port.conf
listen 80;
listen [::]:80;
server_name localhost;
# 请求体大小(包含文件上传)
client_max_body_size 100M;
# 设置根界面重写
location = / {
rewrite ^ /desktop last;
}
# 所有路径设置缓存
location / {
# 禁用缓存
expires -1;
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
add_header Pragma no-cache;
# 过期时间点
add_header Expires "Wed, 11 Jan 1984 05:00:00 GMT";
# 禁用 ETag 和 Last-Modified 头部信息
if_modified_since off;
etag off;
}
# 依赖资源文件
location ~ ^/lib/[^/]+/resources {
root ../../;
index index.html index.htm;
}
# 应用资源文件
location ~ ^/webapps/[^/]+/resources {
root ../../;
index index.html index.htm;
}
# 错误页面
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}root.conf:根路径配置文件
../../../conf/location/root.conf
# nginx自带根路径
# location / {
# root html;
# index index.html index.htm;
# }
# 设置根界面为固定请求路径
# location / {
# # 界面路径
# alias ../../webapps/desktop-1.0/static/;
# index index.html;
# }
# 设置根界面跳转
# location / {
# # 跳转到指定应用
# rewrite ^ /desktop last;
# }
# 设置根界面反向代理
# location / {
# # 代理转发
# proxy_pass http://127.0.0.1:80/desktop/;
# proxy_connect_timeout 60s;
# proxy_read_timeout 180s;
# }3.可视化界面
警告
暂未集成可视化界面
NginxGUI
修改conf文件中的nginx地址
nginx.path = D:\\lib\\nginx-1.24.0
nginx.config = D:\\lib\\nginx-1.24.0\\conf\\nginx.conf用户名:
admin密码:admin