Nginx核心配置
Nginx的配置文件的组成部分:
-
主配置文件:nginx.conf
-
子配置文件: include conf.d/*.conf
-
fastcgi, uwsgi,scgi 等协议相关的配置文件
root 与 alias
root:指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location
server {
listen 80;
server_name www.duxiaoshai.com;
root /home/web/html; index index.html;
location /test1 {
root /home/web;
}
alias:定义路径别名
server {
listen 80;
server_name www.duxiaoshai.com;
root /home/web/html;
index index.html;
location /test1 {
root /home/web;
}
location /test2 {
alias /home/web/test1;
}
}
location 的详细使用
匹配案例-精确匹配
location = /logo.png {
root /webdata/nginx/duxiaoshai/images;
}
匹配案例-区分大小写
location ~ /logo.PNG {
root /webdata/nginx/duxiaoshai/images;
}
匹配案例-不区分大小写
location ~* /logo.PNG {
root /webdata/nginx/duxiaoshai/images;
}
匹配案例-文件名后缀
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js|css)$ {
root /webdata/nginx/duxiaoshai/images; index index.html;
}
匹配案例-URI开始
location ^~ /images {
root /webdata/nginx/duxiaoshai/images;
index index.html;
}
}
location /images1 {
root /webdata/nginx/duxiaoshai/images;
}
#返回内容一样
匹配案例-优先级
location / {
root /webdata/nginx/duxiaoshai/html;
}
location ^~ /images {
root /webdata/nginx/duxiaoshai/images;
index index.html;
}
location /images1 {
root /webdata/nginx/duxiaoshai/images;
}
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/nginx/static3;
index index.html;
}
}
#匹配优先级:=, ^~, ~/~*,/
Nginx 账户认证功能
[root@Nginx ~]# htpasswd -cm /usr/local/nginx/conf/.htpasswd admin
Adding password for user admin
[root@Nginx ~]# cat /usr/local/nginx/conf/.htpasswd admin:$apr1$haGCKgCT$myogggALmqNecTyNupsWQ/
[root@Nginx ~]# mkdir /webdata/nginx/duxiaoshai/login
[root@Nginx ~]# echo login > /webdata/nginx/duxiaoshai/login/index.html
[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name duxiaoshai;
location /login {
root /webdata/nginx/duxiaoshai;
index index.html;
auth_basic "login password";
auth_basic_user_file "/usr/local/nginx/conf/.htpasswd";
}
}
自定义错误页面
[root@nginx1 conf.d]# mkdir -p /data/web/errorpage
[root@nginx1 conf.d]# echo error page > /data/web/errorpage/40x.html
[root@nginx1 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx1 conf.d]# nginx -s reload
[root@nginx1 conf.d]# vim duxiaoshuai.conf
server {
listen 80;
server_name www.duxiaoshai.com;
root /home/web/html;
index index.html;
error_page 404 /40x.html;
location /test1 {
root /home/web;
}
location /test2 {
alias /home/web/test1;
}
location = /40x.html {
root /data/web/errorpage;
}
}
自定义错误日志
server {
listen 80;
server_name www.duxiaoshai.com;
root /home/web/html;
index index.html;
error_page 404 /40x.html;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /test1 {
root /home/web;
}
location /test2 {
alias /home/web/test1;
}
location = /40x.html {
root /data/web/errorpage;
}
}
检测文件是否存在
server {
listen 80;
server_name www.duxiaoshai.com;
root /home/web/html;
index index.html;
error_page 404 /40x.html;
* try_files $uri $uri.html $uri/index.html /error/default.html;
location /test1 {
root /home/web;
}
location /test2 {
alias /home/web/test1;
}
location = /40x.html {
root /data/web/errorpage;
}
}
[root@nginx1 conf.d]# mkdir /home/web/html/error
[root@nginx1 conf.d]# echo "error default" > /home/web/html/error/default.html
[root@nginx1 conf.d]# curl 192.168.58.147
error default
长连接配置
keepalive_timeout timeout [header_timeout]; #设定保持连接超时时长,0表示禁止长连接, 默认为75s #通常配置在http字段作为站点全局配置 keepalive_requests 数字; #在一次长连接上所允许请求的资源的最大数量 #默认为100次,建议适当调大,比如:500
keepalive_timeout 65;
keepalive_requests 2;
测试:
[root@nginx1 nginx]# curl -v 192.168.58.147 80
* Trying 192.168.58.147:80...
* Connected to 192.168.58.147 (192.168.58.147) port 80 (#0)
> GET / HTTP/1.1
> Host: 192.168.58.147
> User-Agent: curl/7.79.1
[root@nginx1 nginx]# telnet 192.168.58.147 80
Trying 192.168.58.147...
Connected to 192.168.58.147.
Escape character is '^]'.
GET / HTTP/1.1
Host: 192.168.58.147
HTTP/1.1 200 OK
Server: nginx/1.21.5
Date: Sat, 17 Aug 2024 14:39:02 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Sat, 17 Aug 2024 14:23:40 GMT
Connection: keep-alive
ETag: "66c0b26c-e"
Accept-Ranges: bytes
error default
GET / HTTP/1.1
Host: 192.168.58.147
HTTP/1.1 200 OK
Server: nginx/1.21.5
Date: Sat, 17 Aug 2024 14:39:30 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Sat, 17 Aug 2024 14:23:40 GMT
Connection: close
ETag: "66c0b26c-e"
Accept-Ranges: bytes
error default
#请求两次断开连接
作为下载服务器配置
location /download {
root /home/web;
autoindex on; #自动索引功能 #
autoindex_localtime on; #计算文件确切大小(单位bytes),此为默认值,off只显示 大概大小(单位kb、mb、gb)
autoindex_exact_size off; #on表示显示本机时间而非GMT(格林威治)时间,默为为off
limit_rate 1024k; #限速,默认不限速
}