






















nginx-1.25.3.zip)。C:\nginx)C:\nginx\conf\nginx.conf(用记事本或 VS Code 编辑)。http { ... } 块中的 server { ... } 节点,替换为以下内容(核心配置):

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 反向代理配置:将云服务器的80端口请求转发到实体服务器
server {
listen 80; # 云服务器监听的端口(HTTP默认80)
server_name _; # 若绑定域名,替换为你的域名(如 example.com)
# 所有请求转发到实体服务器
location / {
proxy_pass http://192.168.1.100:80; # 实体服务器的内网IP和Web端口
proxy_set_header Host $host; # 传递原始请求的Host头
proxy_set_header X-Real-IP $remote_addr; # 传递客户端真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 错误页面配置(可选)
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 配置HTTPS(可选,推荐)
# server {
# listen 443 ssl;
# server_name example.com; # 绑定的域名
# ssl_certificate C:/nginx/ssl/cert.pem; # SSL证书路径
# ssl_certificate_key C:/nginx/ssl/key.pem; # 证书私钥路径
# location / {
# proxy_pass http://192.168.1.100:80; # 同上,转发到实体服务器
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# }
# }
}

proxy_pass:必须填写实体服务器的 内网 IP+Web 端口(如 http://192.168.1.100:80),确保云服务器能访问该地址。http://192.168.1.100:8080)。云服务器需要开放 80(HTTP)和 443(HTTPS,若配置)端口,否则外部无法访问:
http://1.2.3.4),若能显示实体服务器的网页,则反向代理成功。ping 192.168.1.100)。proxy_pass 地址是否错误,日志文件 C:\nginx\logs\error.log 可查错)。在 Windows 系统的云服务器上实现反向代理,可以通过 Nginx for Windows 或 IIS(Windows 自带) 配置,以下是详细步骤(推荐 Nginx,配置更轻量灵活):
192.168.1.100,Web 服务端口为 80)。
https://nginx.org/en/download.html
nginx的目录最好不要有中文,不建议放到C盘。
配置使用前后端分离。前端静态代码放到../../zym/html中,访问端口是80。后端接口的链接地址是8081端口。
例如访问 http://localhost/data/index.html 实质是访问../../zym/html/data/index.html文件
访问htpp://localhost/api/user/user_info 实质是访问http://localhost:8081/user/user_info接口,注意url中的api会被删掉。对于proxy中url中是否有斜杠,比较复杂,具体规则参看:https://www.jianshu.com/p/fc91f00016e4 。本文附录1也有介绍,完全copy自该链接。
nginx.conf
...
server {
listen 80;
server_name localhost;
location /api/ { #规定好了,接口都是api开头的。
proxy_pass http://localhost:8081/; #对于api的请求,通过nginx转发到8081端口
}
location / {
root ../../zym/html; #放置静态代码的地方
index index.html index.htm; #默认主页
}
server {
启动nginx : start nginx
终止nginx: nginx -s stop
更新conf后需要刷新nginx(这个最常用,一般不会随便终止nginx):nginx -s reload
其对于conf文件的配置与windows相同。只是路径根据实际情况修改一下即可。
http://nginx.org/download/nginx-1.22.1.tar.gz
或者直接用命令wget http://nginx.org/download/nginx-1.22.1.tar.gz
(1)新建目录并解压
cd /usr/local/
mkdir nginx
cd nginx/ # 上传文件(或者用wget下载)
ls
(2)安装
ls
cd nginx-1.22.1
./configure --prefix=/usr/local/nginx # prefix后是安装路径;如果要支持https还需要加上 --with-https_ssl_module
#如果遇到error: the HTTP rewrite module requires the PCRE library.还需要先安装下面的libpcre3 libpcre3.dev
(3)启动
进入安装好的目录 /usr/local/nginx/sbin
./nginx # 启动
./nginx -s stop # 快速停止
./nginx -s quit # 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload # 重新加载配置
结论,简单理解为:代理的地址在端口以后如果有东西(包括目录或者/),转发地址会去除location匹配的目录(根据匹配的字符,如果是/api则去除api,如果是/api/则去除/api/)
如果代理地址到端口就没了(没有目录或/),那么转发地址会保留匹配目录
location /api/{proxy_pass http://127.0.0.1:8080/;}
访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/upload
location /api{proxy_pass http://127.0.0.1:8080/;}
访问地址: www.test.com/api/upload-->http://127.0.0.1:8080//upload
location /api/{proxy_pass http://127.0.0.1:8080;}
访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/api/upload
location /api{proxy_pass http://127.0.0.1:8080;}
访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/api/upload
location /api/{proxy_pass http://127.0.0.1:8080/server/;}
访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/server/upload
location /api{proxy_pass http://127.0.0.1:8080/server/;}
访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/server//upload
location /api/{proxy_pass http://127.0.0.1:8080/server;}
访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/serverupload
location /api{proxy_pass http://127.0.0.1:8080/server;}
访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/server/upload
1.proxy_pass代理地址端口后有目录(包括 / ),转发后地址:代理地址+访问URL目录部分去除location匹配目录
2.proxy_pass代理地址端口后无任何,转发后地址:代理地址+访问URL目录部
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。