Nginx - linux
#默认有两个进程:
#一个主进程master,用户管理、监控worker
#一个工作进程 worker,worker进程可以通过worker_processes属性配置多个,参照计算机硬件,可以设置为n-1,为中间件-1
# worker 抢占机制 NIO模型,达到多路复用的效果
# '$' 代表参数
#
# 结构 main:全局配置,worker_processes就是其中之一
#
#
#用户访问设置 默认为nobody
#user root 设置用户访问为root
#
worker_processes 1;
#默认错误日志的位置,自定义位置需要配置,错误日志配置 notice/info 日志级别(debug/info/warn/error/crit)
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid文件默认的位置,指定需要配置
#pid logs/nginx.pid;
#
# event 配置工作模式以及连接数
#
events {# 事件处理
#模型
#linux 默认使用 use epoll;
#use epoll;
#每个worker允许连接的客户端最大连接数,根据硬件来调优
worker_connections 1024;
}
#
# http 模块相关配置
# server:虚拟主机配置,可以配置多个
# location:路由规则、映射、表达式
# upstream:集群,内网服务器,负载均衡规则
#
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"';
#请求记录(记录着每一次访问nginx的请求),默认文件位置,与log_format对应
#access_log logs/access.log main;
#发送文件,默认是开启的,文件高效传输的
sendfile on;
#默认是关闭的,只有sendfile开启的才有效果,累计文件包大小统一传送,高效传输
#tcp_nopush on;
#客户端连接服务器的超时时间,默认是开启的65秒;
#每一个TCP请求保持65秒,在断开连接前再次创建请求如果连接存在则会复用,不会创建新的连接;
#开启也会暂用服务器的资源,根据计算机硬件调优,浏览器对应参数一般设置为60秒
#设置为0,会禁止复用功能使用
#keepalive_timeout 0;
keepalive_timeout 65;
#css/thml/js等文件压缩,传输更快,但是开启压缩也会消耗服务器性能,提升效率,节约带宽
#gzip on;
#
#gzip_min_length 1; 最小字节限制,单位字节
#
#gzip_comp_level 3;压缩比,定义压缩级别,文件越大压缩越多,但是会消耗cpu性能
#
#gzip_types text/plain; 压缩文件类型,可以设置多个,空格间隔
#虚拟服务器,虚拟主机支持多块配置
server {
#监听的端口号
listen 88;#服务监听
#服务域名
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#listen + server_name匹配后,则会使用相关 location 配置;
#路由,对应的页面
location / {#映射,默认映射nginx index.html页面
root html;
index index.html index.htm;
}
# 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;
# }
#}
#如果配置文件过多,可以用 ’include‘拆分为嵌入文件配置
server {
listen 90;
server_name localhost;
#'/'匹配规则 '/chenyb 等都是可以的
#location 映射可以配置多个,但是匹配规则不可以重复
#如果没有入口文件,可以去掉 index index.html; 这一行
location / {
#入口;
#为了安全可以用alias别名替换root属性
#例如:
# 访问路径:/home/chen/files/img/aaa.png
# location /chen{
# root /home
# }
# 请求访问时路径:ip:port/chen/files/img/aaa.png
#
#
#
#alias 可以为路径做一个别名,访问时是透明的
#例如:
# 访问路径:/home/chen/files/img/aaa.png
# location /test{
# alias /home/chen
# }
# 请求访问时路径:ip:port/test/files/img/aaa.png
#
# /test 会对 alias 后的入口路径最后一层替换
root tomcat\webapps\ROOT;
# 首页文件,作为该映射的首页,没有可以去掉
index index.html;
}
}
}
- 常用命令
- ./nginx -h /./nginx -?: 帮助;
- ./nginx -t : 检测配置对不对,能否通过,测试是否编译正常;
- ./nginx -s reload :重新编译,加载配置文件;
- ./nginx :启动nginx;
- ./nginx -s stop:停止,暴力停止当前服务,断开所有连接;
- ./nginx -s quit:优雅的停止,没有请求、响应才会停止,有连接会等通讯完毕,只针对http请求优雅;
- ./nginx -v:查看nginx当前版本;
- ./nginx -V:查看nginx详细信息,包含文件配置信息;
- ./nginx -c:指定特定的配置文件,指定加载配置文件,主要是针对nginx.conf;