nginx常用命令及配置

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值