Nginx

  • 必备的 web 服务器,稳定和高性能,开源免费
  • 一般用于做静态服务、负载均衡
  • 反向代理(对客服端不可见的代理)

安装

官网下载open in new window

配置

反向代理配置:

配置文件路径: nginx-1.20.1 > conf > nginx.conf

server {
	listen       80;
	server_name  localhost;

	location / {
		proxy_pass 	http://localhost:8084;
	}

	location /api/ {
		proxy_pass 	http://localhost:8000;
		proxy_set_header 	Host 	$host;
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13

注意端口

Nginx 服务运行之后默认监听的端口是80,客户端所有请求都先到 Nginx 服务,再去请求静态资源文件,或者反向代理到其它服务上。

其它服务都只在内部运行,端口号不对外开放,对外开放的只有 Nginx 的80端口,这样更加安全。

History 模式

location /projectName {
  try_files $uri $uri/ /projectName/index.html;
}
1
2
3

Node 服务器

location ^~/node/ {
	proxy_pass http://localhost:7001/;
}
1
2
3

常用命令

MAC:

  • sudo nginx -t: 测试配置文件格式是否正确
  • sudo nginx: 启动
  • sudo nginx -s reload: 重启
  • sudo nginx -s stop: 停止

Window:

  • start nginx: 启动
  • nginx.exe -s reload: 重启
  • nginx.exe -s stop: 停止
  • taskkill /IM nginx.exe /F: 关闭所有程序

Mac 系统

  • 查找安装路径:which nginx

  • 安装路径:/usr/local/bin/nginx

  • 配置文件路径:/usr/local/etc/nginx/nginx.conf

  • 静态页面地址:/usr/local/var/www

查看是否启动成功

ps -ef|grep nginx

停止进程

sudo kill -QUIT [id]

日志

日志拆分

  • logrotate
Last Updated: 2023/3/20 13:48:48
Contributors: licong96, 黎聪