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

相关推荐
higherzjm2 天前
如何通过Linux命令自动切割nginx和redis日志
linux·redis·nginx
小张同学a.3 天前
zabbix企业级监控平台3——服务监控与API实战
linux·运维·mysql·nginx·tomcat·zabbix
Archy_Wang_13 天前
LVS+KeepAlived+Nginx实现企业级业务高可靠部署的实战教程
运维·nginx·lvs
Ai拆代码的曹操3 天前
Nginx upstream keepalive 连接池中毒排查实录
chrome·nginx·php
空谷有来人4 天前
Ubuntu22.04上安装配置站点
nginx
PC2005-cloud4 天前
KubeSphere学习笔记:部署nginx
笔记·学习·nginx
后除4 天前
Debian 安装与使用 Certbot
后端·nginx·https
风雨_834 天前
轻量级 Nginx 日志分析和可视化平台 NginxPulse安装
运维·nginx
若无情我 纸小铭4 天前
Nginx学习笔记(二) Nginx--connection&request
笔记·学习·nginx
xixingzhe24 天前
net::ERR_INCOMPLETE_CHUNKED_ENCODING解决
nginx