【本地调试】使用 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。

相关推荐
*郑*6 小时前
nginx配置反向代理后端
运维·nginx
霍志杰9 小时前
网址为 http://xxx:xxxx/的网页可能暂时无法连接,或者它已永久性地移动到了新网址
nginx
hnlucky11 小时前
《基于 Kubernetes 的 WordPress 高可用部署实践:从 MariaDB 到 Nginx 反向代理》
运维·数据库·nginx·云原生·容器·kubernetes·mariadb
铁锚12 小时前
一个WordPress连续登录失败的问题排查
java·linux·服务器·nginx·tomcat
猴子请来的逗比48916 小时前
tomcat与nginx之间实现多级代理
java·nginx·tomcat
matrixlzp19 小时前
Nginx 源码安装成服务
nginx·云原生
π大星星️1 天前
HAProxy + Keepalived + Nginx 高可用负载均衡系统
运维·nginx·负载均衡
Johny_Zhao1 天前
K8S+nginx+MYSQL+TOMCAT高可用架构企业自建网站
linux·网络·mysql·nginx·网络安全·信息安全·tomcat·云计算·shell·yum源·系统运维·itsm
专注代码七年1 天前
在Windows 境下,将Redis和Nginx注册为服务。
windows·redis·nginx
xixingzhe21 天前
Nginx 配置多个监听端口
服务器·前端·nginx