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

相关推荐
fydw_71510 小时前
生产环境中安装和配置 Nginx 以部署 Flask 应用的详细指南
运维·nginx·flask
xzh10 小时前
问题:Nginx client_body_temp_path 文件会删除吗,删除时机?
nginx·架构
dessler11 小时前
代理服务器-LVS的3种模式与调度算法
运维·服务器·网络·算法·nginx·tomcat·lvs
2501_9111212315 小时前
Nginx+Tomcat 负载均衡群集
nginx·tomcat·负载均衡
小鱼小鱼.oO1 天前
阿里云服务器安装nginx并配置前端资源路径(前后端部署到一台服务器并成功访问)
服务器·nginx·阿里云
广东数字化转型1 天前
nginx怎么使用nginx-rtmp-module模块实现直播间功能
linux·运维·nginx
�FENG2 天前
LVS、NGINX、HAPROXY的调度算法
nginx·lvs·haproxy·调度算法
哈哈哈哈哈哈哈哈哈...........2 天前
【Nginx】使用 Nginx+Lua 实现基于 IP 的访问频率限制
tcp/ip·nginx·lua
Adorable老犀牛2 天前
负载均衡将https请求转发后端http服务报错:The plain HTTP request was sent to HTTPS port
nginx·http·https·负载均衡
�FENG2 天前
Nginx+Tomcat负载均衡与动静分离架构
nginx·tomcat·负载均衡·动静分离