nginx 配置文件
2022-10-23
nginx 自动生成 配置文件
https://www.digitalocean.com/community/tools/nginx?global.app.lang=zhCN
可以自由选择所需的应用,生成nginx配置文件作为参考;
nginx 配置区域划分
user www;
http {
数据传输性能相关的参数;
sever{
定义网站的处理,根目录,静态数据存放的目录
}
}
nginx 配置文件讲解
# 全局设置
######## Nginx Main 区,核心功能模块
#定义Nginx运行的用户和用户组,来指定Nginx Worker进程运行用户以及用户组,默认由nobody账号运行
#user nobody;
# nginx 工作是 一个主进程和多个工作进程。工作进程是单进程的,且不需要特殊授权即可运行;
# 这里定义的是工作进程数量,这里建议根据 cpu 内核来定义,4个cpu内核定义4个进程;也可以将进程锁定cpu核心
worker_processes 1;
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ],其中debug输出日志最为最详细,而crit输出日志最少
# 全局错误日志的位置及格式
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程文件,用来指定进程id的存储文件位置
pid logs/nginx.pid;
#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致,可以使用命令“ulimit -n 65535”来设置其他值。
worker_rlimit_nofile 4864;
######## Nginx Main 区
######## Nginx events 区 ,核心功能模块
#工作模式与连接数上限
events {
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
use epoll; #mac平台用kqueue,对于Linux系统,epoll工作模式是首选
#worker_connections用于定义Nginx每个进程的最大连接数,即接收前端的最大请求数,默认是1024。最大客户端连接数由worker_processes和worker_connections决定,即Max_clients=worker_processes*worker_connections,在作为反向代理时,Max_clients变为:Max_clients = worker_processes * worker_connections/4。 进程的最大连接数受Linux系统进程的最大打开文件数限制,在执行操作系统命令“ulimit -n 65536”后worker_connections的设置才能生效
worker_connections 1024;
}
######## Nginx events 区
######## Nginx Http 区,核心模块
#设定http服务器
http {
#来用设定文件的mime类型,类型在配置文件目录下的mime.type文件定义,来告诉nginx来识别文件类型。
include mime.types;
default_type application/octet-stream; #默认文件类型
#charset utf-8; #默认编码
#用于设置访问日志的格式,和记录哪些参数,这里设置为main,刚好用于access_log来纪录这种类型
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#$remote_addr 与$http_x_forwarded_for 用以记录客户端的 ip 地址
#$remote_user:用来记录客户端用户名称;
#$time_local:用来记录访问时间与时区;
#$request:用来记录请求的 url 与 http 协议;
#$status:用来记录请求状态;成功是 200,
#$body_bytes_sent:记录发送给客户端文件主体内容大小;
#$http_referer:用来记录从哪个页面链接访问过来的;
#$http_user_agent:记录客户浏览器的相关信息;
#全局访问日志路径
access_log logs/access.log main;
#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
# 实现用户态到内核态的 0 拷贝;不用拷贝,给个指针直接引用;
sendfile on;
autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。⚠️:只是在没有 index 索引页面时会展示出来
# 防止网络阻塞,
# 此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在sendfile on 的时候使用;
tcp_nopush on;
tcp_nodelay on; #防止网络阻塞
#长连接超时时间,单位是秒
keepalive_timeout 65;
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
# gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml;
#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用
# 支持 include 导入子配置文件
include /etc/nginx/conf.d/*.conf;
upstream blog.ha97.com {
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}
# server 区块
#虚拟主机的配置
server {
#监听端口, 默认的虚拟主机;
listen 80 default_server;
#域名可以有多个,用空格隔开
server_name www.***.com ***.com;
# 设置虚拟主机支持的字符集
#charset koi8-r;
# 虚拟主机的访问日志路径,
#access_log logs/host.access.log main;
# 索引页面
index index.html index.htm index.php;
# server 中定义root,指定整个 server 的网站根目录
root /data/www/***;
# location 区块
# location 表示 访问请求过来的 url 地址;举例:https://www.test.com/
location / {
# 网站根目录路径,相对于nginx安装目录;如果 server 和 location 同时指定 root,优先使用 location 中指定的 root
root html;
# 索引页
index index.html index.htm;
}
# 错误页面,对应状态码 404 ,返回 根下的 404.html
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
# 如果错误码 500 502 503 504 ,返回 根下的 50x.html
error_page 500 502 503 504 /50x.html;
# 定义错误页面路径,= 精确匹配
location = /50x.html {
root html;
}
# 定义反向代理服务器,数据服务器模型是lamp模型
# ~ 模糊匹配,
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# 127.0.0.1 是lamp 的ip地址;nginx 作为反向代理,数据服务器模型为 lamp 模型;
# proxy_pass http://127.0.0.1;
#}
# 定义PHP为本机服务的模型,本机装了 php 程序,
# 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;
#}
# 拒绝nginx DR目录及子目录下的.htaccess文件访问;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# 如果配置一个虚拟主机,基于 ip、域名、端口 都可以;
# 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 的配置
# 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;
# }
#}
}