nginx目录
conf:配置目录
- nginx.conf:nginx配置文件
html:默认站点目录,出现502时,会调用50x.html
- 50x.html:出现502时,会显示此页面
- index.html
logs:
- access.log:访问日志文件,可以查看网站用户访问情况信息
- error.log:错误日志文件,包括nginx启动故障
sbin:nginx命令目录
nginx配置文件
user root; #以root用户权限来运行nginx
worker_processes 1; #要开启的nginx进程数量,通常是cpu内核数量的整数倍
#error_log logs/error.log;
#error_log logs/error.log info; #错误日志的位置及输出级别【debug/info/notice/warn/error/crit】
#pid logs/nginx.pid; #pid文件的存储位置
events {
worker_connections 1024; #单个进程的最大连接数
}
http {
include mime.types; #Nginx支持的媒体类型库文件
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;
# 负载均衡
upstream gateway {
least_conn; # 客户端请求分配到连接数最少的服务器中,如果去掉这一行就是轮询
server 192.168.99.99;
server 192.168.99.100;
}
# 配置一个服务器
server {
listen 8111; # 监听服务器的端口
server_name localhost; # 192.168.1.100,这是服务器的ip
#charset utf-8;
#access_log logs/host.access.log main;
root /opt/front/dist; #当前服务器web项目的目录
index production.html index.html index.htm; # 用户访问web网站时的全局首页
location / {
#root /opt/html; # 写在外面,控制所有的入口,写在location里面,只控制对应入口
#alias /opt/static; # 和root不一样,alias不会拼接URL中的路径,root会拼接
#index index.html index.htm;
}
error_page 404 /40x/404.html; #返回404时,显示该页面,实际路径为/opt/front/dist/40x/404.html
error_page 500 502 503 504 /50x.html;
location = /50x.html { # =是精确匹配,~是正则匹配
root html;
}
location /api {
rewrite ^/api/(.*) /$1 break; #break:重定向后会跳出location作用域,如果访问是/api/abc,$1就是abc
proxy_pass http://gateway;
}
}
}
nginx命令
命令 | 功能 |
---|---|
./nginx | 启动nginx |
./nginx -s reload | 重新加载配置文件 |
./nginx -s stop | 停止nginx |