主配置文件:nginx.conf
子配置文件: include conf.d/*.conf
fastcgi, uwsgi,scgi 等协议相关的配置文件
mime.types:支持的mime类型,MIME(Multipurpose Internet Mail Extensions)多用途互联网
邮件扩展类型,MIME消息能包含文本、图像、音频、视频以及其他应用程序专用的数据,是设定
某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自
动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方
式。
MIME参考文档:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_Types
directive value [value2 ...];
注意
(1) 指令必须以分号结尾
(2) 支持使用配置变量
内建变量:由Nginx模块引入,可直接引用
自定义变量:由用户使用set命令定义,格式: set variable_name value;
引用变量:$variable_name
配置基础配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
### 测试配置文件是否正常
nginx -t
nginx: the configuration file /data/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx-1.10.3/conf/nginx.conf test is successful
shell> curl -I http://localhost
HTTP/1.1 200 OK
nginx监控
开启nginx监控
#设定查看Nginx状态的地址
location /status {
stub_status on; #表示开启stubStatus的工作状态统计功能。
access_log off; #access_log off; 关闭access_log 日志记录功能。
#auth_basic "status"; #auth_basic 是nginx的一种认证机制。
#auth_basic_user_file conf/htpasswd; #用来指定密码文件的位置。
}
配置登录密码
#安装相关包
yum -y install httpd-tools
#创建用户
#-b 非交互式方式提交密码
htpasswd -cd /apps/nginx/conf/.htpasswd user1 123456
Adding password for user user1
htpasswd -b /apps/nginx/conf/.htpasswd user2 123456
Adding password for user user2
tail /apps/nginx/conf/.htpasswd
user1:$apr1$Rjm0u2Kr$VHvkAIc5OYg.3ZoaGwaGq/
user2:$apr1$nIqnxoJB$LR9W1DTJT.viDJhXa6wHv.
vim /apps/nginx/conf/conf.d/pc.conf
location = /login/ {
root /data/nginx/html/pc;
index index.html;
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
}
#重启Nginx并访问测试
curl http://user1:123456@www.magedu.org/login/
login page
curl -u user2:123456 www.magedu.org/login/
login page
nginx全局变量
$args:这个变量等于请求行中的参数,同$query_string。
$is_args: 如果已经设置$args,则该变量的值为"?",否则为""。
$content_length: 请求头中的Content-length字段。
$content_type: 请求头中的Content-Type字段。
$document_uri: 与$uri相同。
$document_root: 当前请求在root指令中指定的值。
$host: 请求主机头字段,否则为服务器名称。
$http_user_agent: 客户端agent信息。
$http_cookie: 客户端cookie信息。
$limit_rate: 这个变量可以限制连接速率。
$request_method: 客户端请求的动作,通常为GET或POST。
$remote_addr: 客户端的IP地址。
$remote_port: 客户端的端口。
$remote_user: 已经经过Auth Basic Module验证的用户名。
$request_body_file`: 客户端请求主体的临时文件名。
$request_uri: 请求的URI,带参数
$request_filename: 当前请求的文件路径,由root或alias指令与URI请求生成。
$scheme: 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect;。
$server_protocol: 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr: 服务器地址,在完成一次系统调用后可以确定这个值。
$server_name: 服务器名称。
$server_port: 请求到达服务器的端口号。
$request_uri: 包含请求参数的原始URI,不包含主机名,如:/foo/bar.php?arg=baz,它无法修改。
$uri: 不带请求参数的当前URI,$uri不包含主机名,如/foo/bar.html可能和最初的值有不同,比如经过重定向之类的。它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html。
例子:
访问链接是:http://localhost:88/test1/test.php
网站路径是:/var/www/html
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test.php
$document_uri:/test1/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test.php
nginx配置文件主要分为四个部分:
server继承main,location继承server,upstream即不会继承其他设置也不会被继承。
main{ #(全局设置)
http{ #服务器
upstream{} #(负载均衡服务器设置:主要用于负载均衡和设置一系列的后端服务器)
server{ #(主机设置:主要用于指定主机和端口)
location{} #(URL匹配特点位置的设置)
}
}
}
main 全局配置
nginx在运行时与具体业务功能(比如http服务或者email服务代理)无关的一些参数,比如工作进程数,运行的身份等。
user www www;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
error_log /var/logs/nginx_error.log crit;
pid /usr/local/webserver/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events模块
events{
use epoll;
worker_connections 65536;
}
http服务器
http{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
}
HTTP参数之客户端head缓存
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 128k;
client_max_body_size 10m;
client_body_buffer_size 128k;
sendfile on ;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65 :
client_body_timeout 60s;
send_timeout 60s;
HTTP参数之FastCGI参数
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;
fastcgi_cache TEST;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
HTTP参数之gzip模块设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;