前端开发小斋--nginx篇

时间:2019-11-26 09:19:51   收藏:0   阅读:97

安装和启动

  1. Mac上搭建nginx教程

    通过Homebrew 安装nginx brew install nginx

  2. 配置
    添加配置文件在 /usr/local/etc/nginx/servers 目录下 ( 一般都是修改 /usr/local/etc/nginx 目录下nginx.conf 文件, 后来发现nginx.conf 下有 include servers/*; 为了方便管理我本地项目,就把本地项目配置都放到 /usr/local/etc/nginx/servers 目录下 )

  1. 启动
    在终端中输入 ps -ef|grep nginx
    如果执行的结果是
501 15800 1 0 12:17上午 ?? 0:00.00 nginx: master process /usr/local/Cellar/nginx/1.8.0/bin/nginx -c /usr/local/etc/nginx/nginx.conf
501 15801 15800 0 12:17上午 ?? 0:00.00 nginx: worker process
501 15848 15716 0 12:21上午 ttys000 0:00.00 grep nginx

表示已启动成功,如果不是上图结果,在终端中执行

/usr/local/Cellar/nginx/1.8.0/bin/nginx -c /usr/local/etc/nginx/nginx.conf

命令即可启动nginx。??这时候如果成功访问localhost:8080,说明成功安装和启动好了。

?? 启动前确认,8080 端口未占用

  1. 停止

在终端中输入 ps -ef|grep nginx 获取到nginx的进程号,注意是找到“nginx:master”的那个进程号,如下面的进程好是 15800

501 15800 1 0 12:17上午 ?? 0:00.00 nginx: master process /usr/local/Cellar/nginx/1.8.0/bin/nginx -c /usr/local/etc/nginx/nginx.conf
501 15801 15800 0 12:17上午 ?? 0:00.00 nginx: worker process
501 15848 15716 0 12:21上午 ttys000 0:00.00 grep nginx

在终端中输入以下几种命令都可以停止

kill -QUIT 15800 (从容的停止,即不会立刻停止)
Kill -TERM 15800 (立刻停止)
Kill -INT 15800 (和上面一样,也是立刻停止)

  1. 重启

如果配置文件错误,则将启动失败,所以在启动nginx之前,需要先验证在配置文件的正确性 nginx -t -c /usr/local/etc/nginx/nginx.conf,如下表示配置文件正确

  /usr/local/Cellar/nginx/1.8.0/bin/nginx -t -c /usr/local/etc/nginx/nginx.conf
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

重启有两种方法?:

  cd /usr/local/Cellar/nginx/1.8.0/bin/
  ./nginx -s reload
 

实用使用命令

启动:sudo nginx
停止:sudo nginx -s stop
验证:sudo nginx -t /usr/local/nginx/conf/nginx.conf

跳坑报错指南

  1. 在Mac上用brew安装Nginx,然后修改Nginx配置文件,再重启时报出如下错误:
    nginx: [error] invalid PID number “” in “/usr/local/var/run/nginx/nginx.pid”
    解决办法:
$ sudo nginx -c /usr/local/etc/nginx/nginx.conf
$ sudo nginx -s reload

本地nginx测试服务器配置

项目根目录为开发项目下构建后dist, api 为 接口api

 server {
    listen       8090;    #监听端口  可以访问127.0.0.1:8090
    # server_name  test.com;  #这里要是使用需要配本地的host

    #charset koi8-r;
    access_log  logs/k8s.log;
    
    location  = / {
      root   /Users/macbookpro/Downloads/workspace/node-web/dist;  #你项目的根目录
      index  index.html index.htm;
    }
    
    location /api/ {
      proxy_pass   https://api.baidu.com;  #****这里配置nginx代理,填上服务器地址
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
  }

原文:https://www.cnblogs.com/yc8930143/p/11933015.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!