mac 配置Nginx服务

2020-08-26  本文已影响0人  nero_i

安装nginx

brew install nginx
安装后使用nginx -v查看nginx版本

配置nginx

1、进入nginx的安装目录,打开nginx.conf文件

cd /usr/local/etc/nginx/
sudo vi nginx.conf

2、对nginx.conf文件进行配置,详解:

user  tongtong staff;
#user root;
# 在配置文件的顶级main部分,代表worker角色的工作进程个数
worker_processes  1;
 
# 错误日志
error_log   /var/log/nginx/error_log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
 
# 进程文件
#pid        logs/nginx.pid;
 
events {
    # 写在events部分,每一个worker进程能并发处理(发起)的最大连接数
    worker_connections  1024;
}
 
http {
    # 文件扩展名与文件类型映射表
    include       mime.types;
     
    # 设定默认文件类型
    default_type  application/octet-stream;
 
    # 为nginx服务器设置详细的日志格式
    #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记录了哪些用户,哪些页面以及用户浏览器,ip和其他的访问信息,access_log路径
    #access_log  logs/access.log  main;
     
    # 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    sendfile        on;
 
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
 
    # 配置信息文件夹
    include conf.d/*.conf;
 
    # 虚拟主机配置
    server {
        # 监听端口 如果有别的服务占用,可以修改
        listen       8080; 
       
        # 域名设定,可以有多个
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            #可以设置绝对自己创建目录路径 /User/black/dist
            root   html;
            # 定义路径下默认访问的文件名
            index  index.html index.htm;
            #解决子路由页面刷新404问题
            try_files $uri $uri/ /index.html;
           
            #转发后端站点地址,一般用于做软负载,轮询后端服务器
            #proxy_pass http://10.11.12.237:8080;
            # 打开目录浏览功能,可以列出整个目录
            autoindex on;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
             
        # 指定FastCGI服务器监听端口与地址,可以是本机或者其它
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 
        # fastcgi配置
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #允许/禁止某个ip/ip段访问文件
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 
}

3、测试配置是否成功
修改完成后保存,使用以下命令检查一下配置文件是否正确,后面是nginx.conf文件的路径,successful就说明正确了

nginx -t -c nginx目录/conf/nginx.conf
成功输出结果
之后就打开浏览器访问刚才的域名及端口http://localhost:8080,出现欢迎页就说明部署成功了
4、开启nginx 服务
sudo nginx
输入本地服务

4、关闭nginx服务使用以下命令,同样也是一闪而过是正常的,看一下是否进程已消失即可
快速停止

sudo nginx -s stop

完整有序的关闭

sudo nginx -s quit

修改配置文件路径:/usr/local/etc/nginx/nginx.conf
nginx默认部署路径:/usr/local/Cellar/nginx/1.15.11/html

上一篇 下一篇

猜你喜欢

热点阅读