nginx一直是web server的必备神器,以稳定和高性能著称

  • 静态服务
  • 反向代理
  • 负载均衡(课程暂时用不到)
  • access log

    安装

    常用命令

  • 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;

  1. keepalive_timeout 65;

gzip on;

include /etc/nginx/conf.d/*.conf;

  1. # www.imooc-lego.com
  2. server {
  3. listen 80;
  4. server_name imooc-lego-editor;
  5. charset utf-8;
  6. index index.html;
  7. root /home/work/lego-team/editor; # 服务根目录
  8. location / {
  9. try_files $uri $uri/ /index.html; # 支持前端 h5 history 路由
  10. }

}

admin.imooc-lego.com

server { listen 8000; server_name imooc-lego-admin; charset utf-8;

  1. # 静态服务
  2. location / {
  3. index index.html;
  4. root /home/work/lego-team/admin-fe/dist;
  5. }
  6. # 后端api服务 /api/是反向代理
  7. location /api/ {
  8. proxy_pass http://127.0.0.1:3003;
  9. proxy_set_header Host $host: $server_port;
  10. proxy_set_header X-Real-IP $remote_addr;
  11. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  12. }

} } ```