1.安装homebrew (mac 包管理工具)
在终端输入命令:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
测试是否安装成功:brew -v
2.安装nginx
brew install nginx
测试是否安装成功: nginx -v
3.启动nginx
brew services start nginx
在浏览器输入localhost:8080 会提示nginx欢迎界面,则表示启动成功
4.打开nginx.conf配置文件
在终端输入:vim /usr/local/etc/nginx/nginx.conf
首先需要进入文件编辑模式;
i : 编辑模式(输入命令i,左下角出现Insert 则表示可以编辑文件);
esc :则表示取消编辑;
添加如下服务器配置(注意:需要和其他server平级):
server {
listen 2000; //监听端口
server_name 192.168.0.101; //本机的ip地址
add_header Access-Control-Allow-Origin *; //允许跨域
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Conten t-Type,Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
location / {
proxy_pass http://www.xxx.com; //远程服务器
proxy_set_header Cookie "JSESSIONID=E25D8A10E4614F9466F1DA410BD3FF"; // 需要的cookie,需要从访问的接口地址找到
}
}
5.保存并退出
命令行输入(:wq)
6.重新加载配置文件
命令行输入:sudo nginx -s reload