
在Mac电脑上运行Nginx,最简便且推荐的方法是使用 Homebrew 安装和管理。以下是详细步骤指南:
打开终端(Terminal),执行以下命令安装 Homebrew(Mac 上最常用的包管理工具):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"安装完成后,执行:
brew --version
brew install nginx安装完成后,可查看版本确认:
nginx -v
默认安装路径为:
/usr/local/etc/nginx/nginx.conf(Apple Silicon 为 /opt/homebrew/etc/nginx/nginx.conf)/usr/local/var/www(或 /opt/homebrew/var/www)brew services start nginx或手动启动:
nginx打开浏览器访问:
http://localhost:8080如果看到 “Welcome to nginx!” 页面,说明运行成功。
操作 | 命令 |
|---|---|
启动 |
|
重启 |
|
停止 |
|
重新加载配置 |
|
测试配置 |
|
编辑配置文件:
nano /usr/local/etc/nginx/nginx.conf添加一个简单的虚拟主机:
server {
listen 8080;
server_name localhost;
location / {
root /usr/local/var/www;
index index.html;
}
}保存后执行:
nginx -s reloadbrew services start nginxHomebrew 会自动将其注册为 launchd 服务,实现开机自启。
brew uninstall nginx本次项目需要做的实操事情
location / {
root /opt/homebrew/var/www;
index index.html index.htm;
}
使用nginx命令启动之后
nginx -t # 语法检查
nginx -s reload将前端静态资源直接打包成dist文件夹,然后使用下述命令,复制的是“dist 里面的文件”,而不是 dist 文件夹本身
# 进入 dist
cd dist
# 把内部所有文件复制到 nginx 根目录
cp -r * /opt/homebrew/var/www/
server {
listen 8080;
server_name localhost;
location / {
root /opt/homebrew/var/www;
index index.html index.htm;
}
location /kb/ {
proxy_pass http://localhost:10003;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Custom-Header custom-value; # 添加自定义头
}
location /user/ {
proxy_pass http://localhost:10003;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Custom-Header custom-value; # 添加自定义头
}
location /assistant/ {
proxy_pass http://localhost:10003;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Custom-Header custom-value; # 添加自定义头
}
location /kl/ {
proxy_pass http://localhost:10003;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Custom-Header custom-value; # 添加自定义头
}
location /platform/ {
proxy_pass http://localhost:10003;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Custom-Header custom-value; # 添加自定义头
}
}原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。