server {
listen 80;
server_name gg.com;
root "D:/mywork/myd2admin/dist";
#允许跨域 (服务端部署时)
# add_header 'Access-Control-Allow-Origin' '*';
# add_header 'Access-Control-Allow-Credentials' 'true';
# add_header 'Access-Control-Allow-Methods' '*';
# add_header 'Access-Control-Allow-Headers' '*';
location / {
index index.php index.html error/index.html;
error_page 400 /error/400.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 500 /error/500.html;
error_page 501 /error/501.html;
error_page 502 /error/502.html;
error_page 503 /error/503.html;
error_page 504 /error/504.html;
error_page 505 /error/505.html;
error_page 506 /error/506.html;
error_page 507 /error/507.html;
error_page 509 /error/509.html;
error_page 510 /error/510.html;
# include D:/mywork/myd2admin/dist/nginx.htaccess;
autoindex off;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
} #前端独立部署时,使用请求转发的方式
location /api/{
proxy_pass http://www.nj.com/api/;
}
location /new-api/helper {
proxy_pass http://127.0.0.1:8089/api;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
停止和重载
当nginx启动后,可以使用“-s”参数向nginx管理进程发送信号来控制nginx:
nginx -s signal
其中,signal可以是以下值:
stop:快速关闭
quit:安全关闭
reload:重载配置文件
reopen:重新打开一个log文件,用于日志切割
nginx启动的bat脚本。(nginx.bat)
:start
set choice=0
echo 1-启动,2-重载,3-退出
set /p choice=请输入你的选择:
echo\
if %choice%==1 (
start nginx && goto success || goto failure
)
if %choice%==2 (
nginx -s reload && goto success || goto failure
)
if %choice%==3 (
nginx -s quit && goto success || goto failure
)
echo 选项无效!请重新选择:
goto start
:failure
echo\
echo 操作失败! 请重新选择:
goto start
:success
::echo 操作成功!窗口交将在2秒后自行关闭。
::ping -n 2 127.0.0.1>nul
::exit
echo 操作成功!请选择:1-关闭窗口 2-继续
set cho=0
set /p cho=请输入你的选择:
if %cho%==1 (
goto exit
)
if %cho%==2 (
goto start
)
goto success
:exit
exit
3. 加斜杠不加斜杆的区别
在 Nginx 的 location 指令中,斜杠(/)用于定义匹配请求 URI 的准则。对于 location /newapi/ 的配置,斜杠的使用有以下含义:
有尾随斜杠 (location /newapi/): 这表示匹配所有以 /newapi/ 开头的请求。这是一个“location prefix”匹配,并且斜杠表示 /newapi/ 是一个目录。对于这样的配置,Nginx 会匹配所有以 /newapi/ 开头的请求,并且在 proxy_pass 指令中,如果没有尾随斜杠,它会将匹配的部分替换为 proxy_pass 中的 URL。
没有尾随斜杠 (location /newapi): 这表示匹配所有以 /newapi 开头的请求,但是与上面的情况不同,这里不一定要求后面跟着斜杠。例如,它会匹配 /newapi、/newapi123 和 /newapi/。没有尾随斜杠的 location 可以匹配更广泛的 URI 模式。
在实践中,这意味着如果你的目的是严格匹配目录和其下的路径,你应该使用尾随斜杠。如果你想要更宽泛地匹配以某个字符串开头的路径,那么你可以省略尾随斜杠。
例如,考虑以下两个配置:
# 有尾随斜杠
location /newapi/ {
proxy_pass https://backend.example.com/;
}
# 没有尾随斜杠
location /newapi {
proxy_pass https://backend.example.com/;
}
对于请求 /newapi/index, 两个 location 都会匹配。但是,如果请求是 /newapiindex,只有第二个 location(没有尾随斜杠的)会匹配,因为它不要求 /newapi 后面必须跟着斜杠。
如果您的 Nginx 配置是这样的:
location /newapi/ {
proxy_pass https://backend.xx.cc/app-api;
}
当请求 /newapi/index/banner/list 到达 Nginx 时,Nginx 会将请求转发到 https://backend.xx.cc/app-api。但是,由于 proxy_pass 指令没有以斜杠(/)结尾,而 location 指令有,这个细微的差别会影响最终代理请求的 URI。
在这种情况下,请求的 URI 的 /newapi/ 部分会被替换为 proxy_pass 中定义的 URI,然后将其余部分附加上去。因此,请求 /newapi/index/banner/list 最终会被代理到 https://backend.xx.cc/app-api/newapi/index/banner/list。
与之前的配置相比:
location /newapi/ {
proxy_pass https://git.xx.com:10988/;
}
在这个配置中,由于 proxy_pass 指令以斜杠(/)结尾,Nginx 会将 location 块中匹配的部分(/newapi/)从请求 URI 中移除,并将剩余的部分附加到 proxy_pass 的 URI 上。所以,请求 /newapi/index/banner/list 会被代理到 https://git.xx.com:10988/index/banner/list。
总结两者的区别:
在第一个例子中,proxy_pass 没有尾随斜杠,导致 Nginx 将 location 匹配的部分替换为 proxy_pass 的 URL 并附加上请求的其余部分。结果是请求的路径会被加倍,例如 /newapi/index/banner/list 变成 /app-api/newapi/index/banner/list。
在第二个例子中,proxy_pass 有尾随斜杠,Nginx 仅附加 location 匹配之后的部分到 proxy_pass 的 URL。结果是请求的路径保持不变,例如 /newapi/index/banner/list 变成 /index/banner/list。