1.常用命令
查看nginx帮助信息
nginx -h
测试nginx是否可用
nginx -t
启动nginx
nginx
重启nginx
nginx -s reload
快速停止nginx
nginx -s stop
完整有序的停止nginx
nginx -s quit
强制终止nginx.exe进程及其所有子进程(最直接)
taskkill /f /t /im nginx.exe
查看nginx启动信息
tasklist /fi "imagename eq nginx.exe"
查看nginx启动端口3000被哪里占用
netstat -o -n -a | findstr :3000
杀掉nginx启动进程端口
taskkill /F /PID 1396
2.配置
location /api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:9082/;
}
这个配置块指定了以 /api/ 结尾的路径,即以斜杠结尾。这意味着它将匹配 /api 路径以及其下的所有子路径,例如 /api/foo、/api/bar 等。所有匹配到的请求都将被代理到 http://localhost:9082/。
去掉http://localhost:9082/的/可以吗?
例如,如果原始请求是 /api/foo,那么:
如果 proxy_pass 是 http://localhost:9082/,代理请求将是 http://localhost:9082/api/foo;
如果 proxy_pass 是 http://localhost:9082,代理请求将是 http://localhost:9082/foo。
注意点:
如果前端代码配置成这样
publicPath: process.env.NODE_ENV === "production" ? "./" : "/",
生成的js,css文件是这样的
配置成
location /dist {
root D:/vue/weather/;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
或者
location / {
root D:/vue/weather/dist/;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
但是当前端配置成
publicPath: process.env.NODE_ENV === "production" ? "/" : "/"
那么生成的js,css文件是这样的
那么nginx只能配成这样, 否则路径不对
location / {
root D:/vue/weather/dist/;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}