目标
启动一个nginx服务,能够代理实际后端接口,并能够屏蔽http请求头中参数字段。
下载
官网下载链接:链接: https://nginx.org/en/download.html
选择windows版本。
配置
将下载的zip包解压后,打开D:…\nginx-1.20.2\conf\nginx.conf,修改server为实际配置。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 809;
server_name localhost;
# localhost主页,直接访问 http://localhost 就会跳转
location / {
root html;
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# nginx代理 springboot项目1
location /get/method1 {
proxy_pass http://localhost:9999/get/method1;
# 关闭所有请求头
proxy_pass_request_headers off;
# 放行Host
proxy_set_header Host $host;
}
# nginx代理 springboot项目2
location /get/method2 {
proxy_pass http://localhost:9999/get/method2;
}
}
}
启动
start nginx
进入目录D:…\nginx-1.20.2
打开cmd,执行start nginx,可以看到一闪而过的弹窗,访问链接: http://localhost:809/,若成功访问Welcome to nginx!页面,即表示启动成功。
关闭
在cmd中执行nginx -s stop可能无效。
那么可以在windows powershell窗口内执行以下命令关闭nginx:
$nginxProcess = Get-Process -Name "nginx"
$nginxProcess.Kill()