nginx入门

1.开始

主要更改端口、ip、代理地址、dist 包路径指向。注意语法和空格分号!!!每次修改 nginx.conf 后,建议按顺序执行:

复制代码
nginx -t
nginx -s reload

下载地址:nginx.org/en/download...

2.nginx配置

nginx 复制代码
server {
    # 访问端口
    listen       9000;
    # 服务端接口 ip
    server_name  192.168.100.100;
    underscores_in_headers on;

    # 状态监测与评价-前端
    location /web {
        # 打包后的 dist 文件包存放路径
        alias /var/www/html/dist;
        
        try_files $uri $uri/ /web/index.html;
        index index.html index.htm;
        
        # 添加缓存控制
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }
    }
  
    location ^~/dev-api/ {
        # 服务端接口地址
        proxy_pass http://192.168.100.100:8080/;
        proxy_set_header Host $proxy_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Nginx-Proxy true;
        
        # 添加超时设置
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }
}
bash 复制代码
# 目录结构应该是:
html/
├── dist/
│   ├── index.html
│   ├── js/
│   ├── css/
│   └── assets/
  • 监听端口 : 9000
  • 服务器地址 : 192.168.1.200
  • 前端路径 : /web (通过 alias 指向 html/dist)
  • API 代理 : /dev-api/ → http://192.168.100.200:8080/
  • 浏览器访问 : http://192.168.100.200:9000/web

3.nginx 常见命令(当前 nginx 路径下执行)

bash 复制代码
# 更改配置后需要重新加载配置
nginx -s reload
# 或
systemctl reload nginx

# Nginx 是否正常运行:
netstat -tlnp | grep 9000

# 检查语法是否正常
./nginx -t
# nginx.conf syntax is ok
# nginx.conf test is successful
# 输出正常

# 或者完全重启
nginx -s stop

# nginx启动
start nginx
# 或
# 双击 nignx.exe 会出现黑色窗口一闪而过(正常现象),但不方便停止和重载配置
# 或 linux 系统启动
sudo systemctl start nginx

# 查看运行状态
sudo systemctl status nginx

快速测试

bash 复制代码
# 在 dist 文件夹下创建一个 test.html 文件
echo "<h1>Test Page</h1>" > /usr/local/nginx/html/dist/test.html

# 访问浏览器:http://192.168.1000.200:9000/web/test.html 是否能访问到
相关推荐
飞奔的瓜牛3 小时前
Nginx 实现TCP与UDP转发:新版与老版本配置详解
tcp/ip·nginx·udp
飞奔的瓜牛4 小时前
Nginx 生产环境运维实践:常见问题、关注要点与日常维护
运维·nginx
IT_陈寒7 小时前
Redis那个过期删除策略,差点让我线上崩了
前端·人工智能·后端
jianqiang.xue7 小时前
Espressif-IDE进入维护模式:乐鑫把ESP32开发的宝押给VS Code
前端·css·ide
特创数字科技8 小时前
纯本地离线运行的文档脱敏工具(技术分享)
前端·驱动开发·python
心之语歌8 小时前
uni-app uts原生插件开发:uts 与 Kotlin 交互踩坑实录(附解决方案)
前端·后端
再吃一根胡萝卜8 小时前
DSH 插件数据链路:Host 管数据,Client 只管画
前端
feixing_fx8 小时前
CSS 变量(Custom Properties):构建可扩展的主题切换系统
前端·css·前端框架·html·css3
十有八七10 小时前
手搓 Agent(一):先有大脑——大模型只会做一件事
前端·人工智能·后端
锋行天下10 小时前
「踩坑实录」给 LangGraph Agent 接两个 FastMCP,我被 langchain 偷偷改了工具名
前端·人工智能·后端