Nginx 反向代理

date
May 27, 2024
slug
proxy
status
Published
tags
Website
summary
使用Nginx 反向代理,隐藏端口访问
type
Post
网址
URL

从安装 Nginx 开始配置反向代理

Ubuntu 系统:
1. 更新软件包列表:
sudo apt update
2. 安装 Nginx:
sudo apt install nginx
3. 启动 Nginx:
sudo systemctl start nginx
4. 检查 Nginx 状态:
sudo systemctl status nginx

配置反向代理

1. 创建 Nginx 配置文件:
在 /etc/nginx/conf.d/ 目录下创建一个新的配置文件,例如 chat-plus.conf,并添加以下内容:
💡
2.解释:
  • listen 80;: 监听 80 端口,即 HTTP 默认端口。
  • server_name chat-plus.zzwopenai.com;: 设置域名,所有访问该域名的请求都会被此配置处理。
  • location /: 匹配所有路径,将请求转发到 http://localhost:8080/。
  • proxy_set_header: 设置一些 HTTP 头信息,用于传递客户端真实 IP 和主机名等信息。
3.测试配置:
sudo nginx -t
4. 重启 Nginx:
sudo systemctl restart nginx
5. 访问测试:
通过浏览器访问自定义域名来测试反向代理是否生效。
请注意:
  • 确保域名已经解析到您的服务器 IP 地址。
  • 确保应用程序正在 8080 端口上运行。
  • 根据需要调整配置文件,例如添加 HTTPS 支持、配置缓存等。
 

© chen_yan 2024 - 2025