nginx配置文件nginx.conf文件结构介绍

#user指定Nginx Worker进程运行用户以及用户组,默认由nobody账号运行。
#user nobody;

#worker_processes指定了Nginx要开启的进程数。每个Nginx进程平均耗费10M~12M内存。建议指定和CPU的数量一致即可。
worker_processes 1;

#error_log用来定义全局错误日志文件。日志输出级别有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;

#用来指定进程pid的存储文件位置
#pid logs/nginx.pid;

#events:设定Nginx的工作模式及连接数上限:
#use是个事件模块指令,用来指定Nginx的工作模式Nginx支持的工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll;
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
#其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,不同的是epoll用在Linux平台上,而kqueue用在BSD
#系统中。对于Linux系统,epoll工作模式是首选。

#worker_connections:用于定义Nginx每个进程的最大连接数,默认是1024;
#值得注意的是如果你不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的。
#最大客户端连接数由worker_processes和worker_connections决定,即Max_client=worker_processes*worker_connections。
#在作为反向代理时,max_clients变为:max_clients = worker_processes * worker_connections/4。
#进程的最大连接数受Linux系统进程的最大打开文件数限制,在执行操作系统命令“ulimit -n 65536”后worker_connections的设置才能生效。

#注意:关于ulimit -n 65536,请自行百度

events {
worker_connections 1024;
}

http {
#文件扩展名与类型映射表:实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度。类似于Apache中的include方法。
include mime.types;
#这里设定默认类型为二进制流,也就是当文件类型未定义时使用这种方式,例如在没有配置PHP环境时,Nginx是不予解析的,此时,
#用浏览器访问PHP文件就会出现下载窗口
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"';

这是我们项目中使用的,仅供参考

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host" "$cookie_ssl_edition" '
'"$upstream_addr" "$upstream_status" "$request_time" '
'"$upstream_response_time" ';

用来指定此虚拟主机的访问日志存放路径,最后的main用于指定访问日志的输出格式;

access_log logs/access.log main;

sendfile参数用于开启高效文件传输模式,将tcp_nopush和tcp_nodelay两个指令设置为on用于防止网络阻塞;

sendfile on;
#激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布,积极的作用是减少网络报文段的数量;
#tcp_nopush on;

激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提高I/O性能

tcp_nodelay on;

设置客户端连接保持活动的超时时间。在超过这个时间之后,服务器会关闭该连接,单位是"秒";

keepalive_timeout 0;

keepalive_timeout 65;

开启gzip压缩功能

gzip on;

反向代理负载均衡配置部分

upstream backend_server {
#max_fails:定义的时间段内连接该主机的失败次数,以此来断定 fail_timeout定义的时间段内该主机是否可用。
#默认情况下这个数值设置为 1。零值的话禁用这个数量的尝试。设置在指定时间内连接到主机的失败次数,超过该次数该主机被认为不可用。
#这里是在30s内尝试2次失败即认为主机不可用!
server localhost:80 weight=1 max_fails=2 fail_timeout=30s;
}

虚拟主机配置

server {
#listen用于监听指定虚拟主机的服务端口
listen 80;
#server_name用于指定IP地址或者域名,多个域名之间用空格分开;
server_name localhost;

Charset用于设置网页的默认编码格式

charset koi8-r;

access_log logs/host.access.log main;

ocation URL匹配配置

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;
}

location /api/ {
#代理转发,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走
proxy_pass http://10.102.46.93:18685/api/xy/;
tcp_nodelay on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

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; #限制此路径(或者uri)访问

}

}

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;

}

}

}

posted @ 2022-09-01 09:22  沐芬而行  阅读(173)  评论(0)    收藏  举报