🥷 Node 和 Nginx 都可以作为服务器,Node 常用于上层的业务逻辑处理,而 Nginx 常用于底层的服务器资源处理,例如静态资源处理、正向代理(proxy_pass)、负载均衡(upstream)等。这儿记录一些 Nginx 的基础操作,涉及安装、常见配置、多域名/多站点、单域名/多站点、前端项目基本部署等。
Nginx 安装
通过官方指引能安装到最新的版本。
Nginx 命令
1 2 3 4 5 6 7 8 9 10 11 12 13
| sudo service nginx start sudo service nginx restart nginx -c /path/nginx.conf nginx -s reload nginx -s stop ngins -s quit nginx -t
ps -ef | grep nginx
du -sh file
tail -f error.log
|
Nginx 配置
1 2
| whereis nginx cat /etc/nginx/nginx.conf
|
加载自己的配置文件
nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| user nginx; worker_processes auto;
error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid;
events { use epoll; worker_connections 1024; multi_accept on; }
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;
keepalive_timeout 65; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types application/atom+xml application/geo+json application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/xhtml+xml application/xml font/eot font/otf font/ttf image/svg+xml image/jpeg image/gif image/png text/css text/javascript text/plain text/xml; gzip_vary on; gzip_disable "MSIE [1-6]\."; include /etc/nginx/vhosts/*.conf; }
|
配置 HTTPS 证书和 root 或 proxy_pass,可以参考阿里云。
新建自己的配置文件
zhihur.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| server { listen 443 ssl; server_name zhihur.com;
ssl_certificate vhosts/cert/zhihur.com.pem; ssl_certificate_key vhosts/cert/zhihur.com.key;
ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on; location / { root /workspace/draft; index index.html index.htm; } } server { listen 80; server_name zhihur.com; rewrite ^(.*)$ https://$host$1; location / { index index.html index.htm; } }
|
多域多站点
基于域名的虚拟主机,用的比较多,重点掌握。
域名解析配置。
Nginx 配置
zhihur.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| server { listen 443 ssl; server_name zhihur.com;
ssl_certificate vhosts/cert/zhihur.com.pem; ssl_certificate_key vhosts/cert/zhihur.com.key;
location / { root /workspace/draft; index index.html index.htm; } }
server { listen 80; server_name zhihur.com; rewrite ^(.*)$ https://$host$1; location / { index index.html index.htm; } }
server { listen 443 ssl; server_name test.zhihur.com;
ssl_certificate vhosts/cert/test.zhihur.com.pem; ssl_certificate_key vhosts/cert/test.zhihur.com.key;
location / { proxy_pass http://127.0.0.1:3000; index index.html index.htm; } }
server { listen 80; server_name test.zhihur.com; rewrite ^(.*)$ https://$host$1; location / { index index.html index.htm; } }
|
单域多站点
单域名的情况下,如何部署多站点?
通过端口号区分
基于端口号的虚拟主机,这种一般很少用,了解即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server { listen 81; server_name zhihur.com; location / { root /workspace/a; index index.html index.htm; } }
server { listen 82; server_name zhihur.com; location / { root /workspace/b; index index.html index.htm; } }
|
通过目录区分
zhihur.conf
的配置不变。
1 2 3
| /workspace/draft/a/index.html /workspace/draft/b/index.html /workspace/draft/c/index.html
|
最基本部署
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server { listen 80; server_name yourdomain.com;
location / { root /workspace/vue-project; try_files $uri $uri/ /index.html; index index.html index.htm; }
location /api { proxy_pass http://baidu.com; } }
|