mac安装配置nginx

506 阅读1分钟

一. 前提

必须安装了homebrew,可在终端输入命令brew -v查看是否已经安装,如果输入指令出现版本号说明已经安装成功

二. 下载安装

1. 查看是否已经安装nginx

brew search nginx 

2. 安装

 brew install nginx 

image.png

3. 启动nginx

 #查看进程
 ps -ef|grep nginx
 #验证配置文件是否正确
 sudo /usr/local/Cellar/nginx/1.15.5/bin/nginx -t -c /usr/local/etc/nginx/nginx.conf
 #两种启动
 nginx 
 brew services start nginx
 

4.查看nginx的配置信息

brew info nginx    

image.png

5. 修改配置nginx,解决跨域

server {  
  # 需要被监听的端口号,前提是此端口号没有被占用,否则在重启 Nginx 时会报错  
  listen       8888;  
  # 服务名称,无所谓  
  server_name  localhost;  
  
  # 上述端口指向的根目录  
  root D:/work/codet;  
  # 项目根目录中指向项目首页  
  index index.html;  
  
  client_max_body_size 20m;  
  client_body_buffer_size 128k;  
  
  # 由于路由的资源不一定是真实的路径,无法找到具体文件  
  # 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源  
  location / {  
      proxy_pass https://localhost:8080/;  
  }  
  # 配置你要访问的服务器地址  
  location /grafana/ {  
      proxy_pass https://192.128.29.102/grafana/;  
      proxy_set_header Host $host;  
      proxy_set_header X-Real-IP $remote_addr;  
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
      proxy_set_header   Cookie $http_cookie;  
      # for Ajax  
      #fastcgi_param HTTP_X_REQUESTED_WITH $http_x_requested_with;  
      proxy_set_header HTTP-X-REQUESTED-WITH $http_x_requested_with;  
      proxy_set_header HTTP_X_REQUESTED_WITH $http_x_requested_with;  
      proxy_set_header x-requested-with $http_x_requested_with;  
  }  
  
  location /audit-apiv2{  
      proxy_pass https://192.128.29.102/audit-apiv2/;  
  }  
}

6. 重启nginx

  nginx -s reload

brew services restart nginx
7. 如果要快速停止nginx
nginx -s stop 

ps -ef|grep nginx
kill -QUIT 72 (从容的停止,即不会立刻停止)
Kill -TERM 72 (立刻停止)
Kill -INT 72 (和上面一样,也是立刻停止)

6.访问nginx

默认端口是8080,访问http://localhost:8080/ 就能看到nginx在本计算机搭建的服务器。  

在这里插入图片描述