【本地调试】使用 Nginx 和 Hosts 文件实现本地开发调试请求转发

可以按照以下 nginx 配置来设置,通过 nginx 和 host 将网页的请求转发到本地的后端服务器,以方便本地开发调试

一、nginx 配置

conf 复制代码
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/json;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       8080;	# 这里替换成开发/测试的实际端口
        server_name  your-domain.com;  # 将 your-domain.com 替换为你的实际域名

        error_log /path/to/your/error.log error;  # 将 /path/to/your/error.log 替换为你的实际路径

        location / {
            # 将所有请求转发到本地服务器
            proxy_pass http://127.0.0.1:9006/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

说明

  1. server_name : 将 your-domain.com 替换为你的实际域名。
  2. error_log : 将 /path/to/your/error.log 替换为你的实际日志路径。
  3. proxy_set_header: 这些指令设置了必要的请求头信息,确保请求正确地转发到本地服务器。
  4. error_pagelocation = /50x.html: 配置自定义的错误页面路径。

二、配置 hosts 文件

这里推荐使用 switchhosts 进行 hosts 的编辑,比较方便。

为了让本地开发能够正确地解析到的 Nginx 服务器,需要在本地 hosts 文件中添加一条记录。以管理员身份编辑 hosts 文件,并添加以下内容:

复制代码
127.0.0.1    your-domain.com

your-domain.com 替换为你的实际域名。

三、重启 nginx

例如,mac 通过 brew 安装的 nginx,通过 brew services restart nginx 命令重启 nginx。

相关推荐
lastHertz13 小时前
Nginx安装部署
运维·nginx
开发小程序的之朴20 小时前
安企CMS的安装-PHPStudy(小皮面板)部署
windows·mysql·nginx
MMendex21 小时前
Nginx_day4——uWSGI,灰度发布,网站限流限速,Nginx跨域
运维·nginx
明月心9522 天前
https配置
nginx·https配置
Ai拆代码的曹操2 天前
TCP RST 包全链路定位:从 Connection Reset 错误到 Nginx keepalive_timeout 不匹配
网络协议·tcp/ip·nginx
她叫我大水龙2 天前
nginx 编译指南:静态编译 + 动态编译
运维·nginx
Icoolkj2 天前
ICOOLKJ 前后端分离项目完整部署文档(MySQL8.4+Redis+JDK21+Nginx+HTTPS+自动同步演示库)
redis·nginx·https
白驹过隙不负青春3 天前
OpenResty 1.29.2.5 部署 + Nginx平滑升级运维文档
nginx·openresty
Cry丶3 天前
一次 FTP 上传引发 504 Gateway Timeout 的排查复盘
java·nginx·ftp·openresty·被动模式·504 gateway·生产问题排查
一勺菠萝丶3 天前
生产环境平滑升级实战-Nginx维护页数据库迁移与安全回滚
数据库·nginx·安全