一、Mac Nginx 安装
二、nginx.conf配置文件使用
- /usr/local/etc/nginx/nginx.conf
-
- # M1 系统路径
- /opt/homebrew/etc/nginx/nginx.conf
-
本地 hosts 文件路径:
- /etc/hosts
-
找到配置文件之后,通常我们需要先备份一下,nginx.conf文件拷贝一份改名为nginx.conf.bak,以防万一,我们继续使用nginx.conf文件进行配置调整:- $ cd /usr/local/etc/nginx
- $ egrep -v '#|^$' nginx.conf
-
- # M1 系统路径
- /opt/homebrew/etc/nginx
-
- # Nginx worker 进程个数:其数量直接影响性能。
- # 推荐设置:cpu的个数 * 核心数(几核CPU)
- worker_processes 1;
-
- events {
- # 配置每个工作进程支持的最大连接数(一个进程的并发量)
- worker_connections 1024;
- }
-
- http {
- # 嵌入其他配置文件
- include mime.types;
-
- # 响应文件类型
- default_type application/octet-stream;
-
- # 日志格式
- # 语法:log_format 格式名称 格式样式(可以多个)
- # log_format 与 access_log 既可以配置在 http{ ... }里面,也可以配置在虚拟主机 server{ ... } 里面
- #log_format main '$remote_addr - $remote_user - $http_user_agent';
-
- # 日志文件的存放路径、格式、缓存大小
- # log_format 与 access_log 既可以配置在 http{ ... }里面,也可以配置在虚拟主机 server{ ... } 里面
- #access_log logs/access.log main;
-
- # 是否使用 sendfile 系统调用来传输文件
- sendfile on;
-
- # 超时时间
- keepalive_timeout 65;
-
- # 每一个 server 就是一个虚拟主机,当需要多站点的时候多配置几个 server 即可
- server {
-
- # 监听的端口号
- listen 8080;
-
- # 主机名称,如果是本地电脑测试,记得在 hosts 文件中做好域名解析
- server_name localhost;
-
- # 当浏览器输入地址访问服务器的时候,就会进入到这个里面来匹配,这个配置看文章底部
- location / {
-
- # 匹配的根目录,只写文件名默认在 /nginx 文件夹下
- # 也可以写成绝对路径 root /usr/local/var/dzm;
- root html;
-
- # 设置默认首页,按先后顺序匹配首页
- index index.html index.htm;
- }
-
- # 当报错 500 502 503 504 的时候走这个指定页面
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
-
- # 嵌入其他配置文件
- # 语法:include /path/file 绝对路径,指定目录下的指定文件
- # 语法:include path/file 相对路径,指定目录下的指定文件
- # 语法:include path/* 指定目录下的所有文件
- # 语法:include file 当前 nginx.conf 同文件夹下的指定文件
- # 参数既可以是绝对路径也可以是相对路径(相对于 nginx 的配置目录,即 nginx.conf 所在的目录
- # 本文下面有使用示例 —— nginx.conf 多个虚拟主机使用
- include servers/*;
- }
-
三、测试 mac 本机新增一个虚拟主机
- # 新增一个 server
- server {
- listen 8080;
- server_name www.dzm.com;
- location / {
- root /usr/local/var/dzm;
- index index.html;
- }
- }
-
- # 多站点配置测试
- 127.0.0.1 www.dzm.com
-
配置好之后,需要运行$ nginx -s reload刷新配置文件,然后访http://www.dzm.com:8080路径四、多个虚拟主机使用
- server {
- listen 8081;
- server_name www.xyq.com;
- location / {
- root /usr/local/var/xyq;
- index index.html;
- }
- }
-
- # 多站点配置测试
- 127.0.0.1 www.dzm.com
- 127.0.0.1 www.xyq.com
-
配置好之后,需要运行$ nginx -s reload刷新配置文件,然后访http://www.xyq.com:8081路径五、虚拟主机别名设置
- server {
- listen 8081;
- # 别名只需要在域名后面通过 空格分开之后直接写别名即可
- server_name www.xyq.com xyq.net xyq.cn;
- location / {
- root /usr/local/var/xyq;
- index index.html;
- }
- }
-
- # 多站点配置测试
- 127.0.0.1 www.dzm.com
- 127.0.0.1 www.xyq.com
- 127.0.0.1 xyq.net
- 127.0.0.1 xyq.cn
-
配置好之后,需要运行$ nginx -s reload刷新配置文件,然后访http://www.xyq.com:8081路径六、日志配置跟使用,以及日志路径指定
- http {
- ......
-
- log_format mylog '$remote_addr - $request - $status - $http_user_agent';
- access_log test.access.log mylog;
-
- server {
- ...
- }
- }
-
- /usr/local/Cellar/nginx/1.17.3_1/test.access.log
-
七、location的作用,可以看看Nginx 配置文件(nginx.conf)属性详细总分析中的location 使用实例