Mac 下 Nginx 配置使用

安装 homebrew

 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

使用homebrew安装

sudo brew install nginx

启动 Nginx

sudo nginx

停止 Nginx

sudo nginx -s stop

或者在终端输入
ps -ef | grep nginx
查找 nginx: master process

存在的话直接
pkill -9 nginx

修改配置文件重新加载

sudo nginx -s reload

常用的指令有:

nginx -s reload 重新加载配置
nginx -s reopen 重启
nginx -s stop 停止
nginx -s quit 退出
nginx -V 查看版本,以及配置文件地址
nginx -v 查看版本
nginx -c filename 指定配置文件
nginx -h 帮助
nginx -t 查看配置是否正确

排错思路

一般都是端口问题或配置问题

  • 先说端口问题
    查找配置文件位置在终端输入
sudo nginx -t

红框标注就是配置文件位置
输入cat /usr/local/etc/nginx/nginx.conf
找到红框标注的位置查看端口

找到端口号输入sudo lsof -P -itcp:8080
查看端口被占用情况选择关闭还是更换端口号。

  • 配置
    确定了配置文件位置直接打开更改

贴下我的配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    server {
      listen       8080;
      server_name  localhost;

      #charset koi8-r;

      #access_log  logs/host.access.log  main;

            # 允许请求地址跨域 * 做为通配符
            add_header 'Access-Control-Allow-Origin' '*' always;
            # 设置请求方法跨域
            add_header 'Access-Control-Max-Age' '1000' always;
            # 设置是否允许 cookie 传输
            add_header 'Access-Control-Allow-Methods' "POST, GET, OPTIONS, DELETE, PUT" always;
             # 设置请求头 这里为什么不设置通配符 * 因为不支持
            add_header 'Access-Control-Allow-Headers' "x-requested-with, Content-Type, origin, authorization, accept, client-security-token" always;

            # 设置 options 请求处理
            if ( $request_method = 'OPTIONS' ) {
                return 200;
            }

      location /webShell {
           # 设置反向代理
          proxy_pass http://127.0.0.1:8081;
          proxy_set_header Host $host:$server_port;
          proxy_set_header X-Forwarded-Host $server_name;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }

     location / {
               # 设置反向代理
              proxy_pass http://127.0.0.1:1841;
              proxy_set_header Host $host:$server_port;
              proxy_set_header X-Forwarded-Host $server_name;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          }

      #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;
      }

    }
    include servers/*;
}
原文地址:https://www.cnblogs.com/GuyCui/p/14312886.html