nginx一直是web server的必备神器,以稳定和高性能著称
- 静态服务
- 反向代理
- 负载均衡(课程暂时用不到)
-
安装
常用命令
nginx // 启动
- nginx -s reload
- nginx -s stop
- nginx -t // 测试
- nginx -c xxx.conf // 指定配置
配置
参考www.imooc-lego.com
服务器的 nginx 配置 ```javascript user root; worker_processes auto; # 多进程 error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;
events { worker_connections 1024; }
http { include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
# www.imooc-lego.com
server {
listen 80;
server_name imooc-lego-editor;
charset utf-8;
index index.html;
root /home/work/lego-team/editor; # 服务根目录
location / {
try_files $uri $uri/ /index.html; # 支持前端 h5 history 路由
}
}
admin.imooc-lego.com
server { listen 8000; server_name imooc-lego-admin; charset utf-8;
# 静态服务
location / {
index index.html;
root /home/work/lego-team/admin-fe/dist;
}
# 后端api服务 /api/是反向代理
location /api/ {
proxy_pass http://127.0.0.1:3003;
proxy_set_header Host $host: $server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}