1. 安装
mac上安装套件可以通过homeBrew,按照官网步骤即可完成nginx的安装,默认安装目录为/usr/local/Cellar。进入/use/local/Cellar/nginx目录,启动nginx :sudo nginx ;访问localhost:8080 发现已出现nginx的欢迎页面了,安装成功!
2. 配置静态页面
打开nginx的配置文件 sudo vim /usr/local/etc/nginx/nginx.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /Applications/XAMPP/xamppfiles/htdocs/web;//静态文件目录
index index.html index.htm;//欢迎页面
}
location = /50x.html {
root /Applications/XAMPP/xamppfiles/htdocs/web;
}
location /api {
proxy_redirect off;
proxy_set_header x-forwarded-proto $scheme;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_pass http://edutest.kidcares.cn:8281/api;//请求地址
# proxy_pass http://edutest.kidcares.cn:8081/api;
}
修改完这三个地方,可以通过localhost访问静态页面了,发起请求
3. 修改电脑hosts文件
终端输入sudo vim /etc/hosts
,添加指向的域名
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 edutest.a
127.0.0.1 edutest2.a
127.0.0.1 edudev.a
配置完成啦!
nginx命令:
启动:sudo nginx
关闭:sudo nginx -s stop
重启:sudo nginx -s reload (在开着服务的时候)
查看进程:sudo ps -ef|grep nginx