共计 1650 个字符,预计需要花费 5 分钟才能阅读完成。
本地开发有时候须要调试动态文件资源,无奈间接拜访,能够通过配置本地 Nginx 服务的形式来进行,顺便记录一下 Nginx 的配置步骤
装置
<!-- 通过 Brew 装置:--> | |
brew install nginx | |
<!-- 启动:--> | |
brew services start nginx | |
<!-- 查看配置:--> | |
cat usr/local/etc/nginx/nginx.conf | |
<!-- 编辑配置:--> | |
vi usr/local/etc/nginx/nginx.conf |
Nginx 命令:
<!-- 启动:--> | |
nginx | |
<!-- 进行 / 重启 --> | |
nginx -s stop/start/restart |
配置文件
文件地址:usr/local/etc/nginx/nginx.conf
# 此处配置为 root owner 能力拜访 root 的动态文件,否则会报 403 | |
user root owner; | |
worker_processes 1; | |
#error_log logs/error.log; | |
#error_log logs/error.log notice; | |
#error_log logs/error.log info; | |
#pid logs/nginx.pid; | |
events {worker_connections 1024;} | |
http { | |
include mime.types; | |
default_type application/octet-stream; | |
#log_format main '$remote_addr - $remote_user [$time_local]"$request" ' | |
# '$status $body_bytes_sent"$http_referer" ' | |
# '"$http_user_agent" "$http_x_forwarded_for"'; | |
#access_log logs/access.log main; | |
sendfile on; | |
#tcp_nopush on; | |
#keepalive_timeout 0; | |
keepalive_timeout 65; | |
#gzip on; | |
server { | |
# 监听端口 | |
listen 8080; | |
# 绑定域名 | |
server_name local.XXX.com; | |
#charset koi8-r; | |
#access_log logs/host.access.log main; | |
#文件门路和入口文件 | |
location / { | |
root /usr/local/var/www; | |
index index.html index.htm; | |
} | |
# 接口资源 1 | |
location /XXXapi/ {proxy_pass https://api.XXX.com;} | |
# 接口资源 2 | |
location /apiXXX/ {proxy_pass https://api.XXX.com;} | |
#error_page 404 /404.html; | |
# redirect server error pages to the static page /50x.html | |
# | |
error_page 500 502 503 504 /50x.html; | |
location = /50x.html {root html;} | |
} | |
include servers/*; | |
} | |
配置步骤
- 装置 Nginx
- 通过 SwitchHost 绑定 HOST (127.0.0.1 local.XXX.com)
- 配置端口和域名
# 监听端口 | |
listen 8080; | |
# 绑定域名 | |
server_name local.XXX.com; |
- 指定入口文件和动态文件门路
# 文件门路和入口文件 | |
location / { | |
root /usr/local/var/www; | |
index index.html index.htm; | |
} |
- 如果有额定的 API 资源,通过 proxy_pass 绑定对应的 API 资源地址
# 接口资源 1 | |
location /XXXapi/ {proxy_pass https://api.XXX.com;} | |
# 接口资源 2 | |
location /apiXXX/ {proxy_pass https://api.XXX.com;} |
- 将动态文件放入 Nginx 配置的文件门路
- DONE,本地能够通过对应的 HOST 关上动态网站资源并拜访
正文完