Nginx本地配置部署

nginx为HTTP服务器

一、常用功能

1、Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。

2、Nginx提供的负载均衡策略有2种:内置策略和扩展策略。

3、web缓存

 

二、常用命令:

     nginx 启动服务

     nginx -s stop 停止服务

     nginx -s quit         优雅停止nginx,有连接时会等连接请求完成再杀死worker进程  

     nginx -s reload     优雅重启,并重新载入配置文件nginx.conf

     nginx -s reopen     重新打开日志文件,一般用于切割日志

     nginx -v            查看版本  

     nginx -t            检查nginx的配置文件

     nginx -h            查看帮助信息

     nginx -V       详细版本信息,包括编译参数 

     nginx  -c filename  指定配置文件

三、文件配置

1.查看修改nginx配置文件

/usr/local/etc/nginx/nginx.conf

2.修改本地127.0.0.1对应的地址

/etc/hosts

 

nginx配置文件

1.默认配置

location / {

root html;

index index.html index.htm;

}

默认访问 /usr/local/var/www文件下的index.html

2.自定义访问文件

server{

listen 8082;

server_name localhost;

location / {

root /Users/liuyin/Documents/dist;

index index.html index.htm;

}

}

默认打开/Users/liuyin/Documents/dist目录下的index.html文件

 

3.代理转发

 

upstream todo {

server www.json.cn;

keepalive 64;

}

 

location / {

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $http_host;

proxy_set_header X-Nginx-Proxy true;

proxy_set_header Connection "";

proxy_pass http://todo;

}

location /element/ {

proxy_pass http://element.eleme.cn/#/zh-CN;

}

 

或者

location / {

proxy_pass http://www.json.cn;

}