目录结构
大约 4 分钟约 1168 字
目录结构
[root@localhost ~]# tree /usr/local/nginx
/usr/local/nginx
├── client_body_temp # POST 大文件暂存目录
├── conf # Nginx所有配置文件的目录
│ ├── fastcgi.conf # fastcgi相关参数的配置文件
│ ├── fastcgi.conf.default # fastcgi.conf的原始备份文件
│ ├── fastcgi_params # fastcgi的参数文件
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types # 媒体类型
│ ├── mime.types.default
│ ├── nginx.conf #这是Nginx默认的主配置文件,日常使用和修改的文件
│ ├── nginx.conf.default
│ ├── scgi_params # scgi相关参数文件
│ ├── scgi_params.default
│ ├── uwsgi_params # uwsgi相关参数文件
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp # fastcgi临时数据目录
├── html # Nginx默认站点目录
│ ├── 50x.html # 错误页面优雅替代显示文件,例如出现502错误时会调用此页面
│ └── index.html # 默认的首页文件
├── logs # Nginx日志目录
│ ├── access.log # 访问日志文件
│ ├── error.log # 错误日志文件
│ └── nginx.pid # pid文件,Nginx进程启动后,会把所有进程的ID号写到此文件
├── proxy_temp # 临时目录
├── sbin # Nginx 可执行文件目录
│ └── nginx # Nginx 二进制可执行程序
├── scgi_temp # 临时目录
└── uwsgi_temp # 临时目录
基本配置文件
默认文件
#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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
注释
# 更多配置信息 http://nginx.org/en/docs/
user nginx;
# 工作进程:一般是 cpu有几核就写几,可以最大限度的去发挥它的性能
worker_processes auto;
# 错误日志路径
error_log /var/log/nginx/error.log;
# 千万别动这玩意,是给守护进程用的
pid /var/run/nginx.pid;
# 负载动态模块
include /usr/share/nginx/modules/*.conf
# 并发连接数:最大并发数 -> 一个工作进程下的最大连接【默认 1024】
events {
worker_connections 1024;
}
# http 配置
http {
# 日志格式
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 /var/log/nginx/access.log main;
# sendfile & tcp_nopush & tcp_nodelay的解释 https://www.jianshu.com/p/cac0a92b9530
# 是否允许上传文件
sendfile on;
# 允许把http response header和文件的开始放在一个文件里发布,作用是减少网络报文段的数量
tcp_nopush on;
# 内核会等待将更多的字节组成一个数据包,从而提高I/O性能
tcp_nodelay on;
# gzip 压缩
gzip on;
# 长连接多长时间没有通信自动断开
keepalive_timeout 65;
# 为了快速处理静态数据集,例如服务器名称, 映射指令的值,MIME类型,请求头字符串的名称,nginx使用哈希表
types_hash_max_size 2048;
# 文件扩展名与类型映射表
include /etc/nginx/mime.types;
# 默认文件类型
default_type application/octet-stream;
# 定义反向代理服务器
upstream web{
# 设置后,后面每次访问都是定位到第一次访问到的服务器
ip_hash;
# 这里的 server如果只写一个就是单纯的额外网发布,如果写 n 个就是负载均衡
server 127.0.0.1:8080;
server 127.0.0.1:8888 weight=1; #添加权重
}
#-------------------------------------------------------------------------
# 加载模块化配置文件,可以把下面 server的配置写到 /etc/nginx/conf.d/ 路径下的某个文件👇
# 👆 就可以直接引入,不需要在这个文件写server的配置
include /etc/nginx/conf.d/*.conf;
#-------------------------------------------------------------------------
# 一个 server对应一个网站
server {
# 监听端口
listen 80 default_server;
listen [::]:80 default_server;
# server域名
server_name localhost;
# 站点根目录,即网站程序存放目录
root /usr/share/nginx/html;
# 默认服务器块的加载配置文件
include /etc/nginx/default.d/*.conf;
# 对“/”启用反向代理
location / {
root html;
index index.html index.htm;
}
# 对“/xxx/”启用反向代理
location /xxx/ {
# 过来的请求代理到哪里,web为前面upstream定义的
proxy_pass http://web;
# 如果需要客户端 ip,这个开关可能会重写为反向代理的 ip
proxy_redirect off;
# nginx 可能会改写头,用原来的值再把它改回来
proxy_set_header Hose $host;
# 代理服务器转发请求的时候用的协议版本
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
# 取客户端真实 ip
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 超时
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 配置 https
server {
# 一定要带上 ssl 标记,默认 443 端口
listen 443 ssl;
server_name work.com;
ssl on;
# 证书
ssl_certificate /etc/nginx/server.crt;
# 密钥
ssl_certificate_key /etc/nginx/server.key;
# 超时
ssl_session_timeout 5m;
location / {
root /usr/local/web/;
add_header 'Cache-Control' 'no-store';
}
}
}