一、 Nginx配置文件结构
Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件。 整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始于结束。 1. Main层位于nginx.conf配置文件的最高层 2. Main层下可以有Event、HTTP层 3. HTTP层下面允许有多个Server层,用于对不同的网站做不同的配置 4. Server层允许有多个Location,用于对不同的路径进行不同模块的配置[root@my-node10 nginx]# cat nginx.conf user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
[root@my-node10 conf.d]# cat default.conf server { listen 80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/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 /usr/share/nginx/html; } # 省略其他的配置 }
二、 日志配置
[root@my-node10 nginx]# pwd /application/nginx [root@my-node10 nginx]# tree . ├── hotel │ └── index.html └── shop └── index.html 2 directories, 2 files [root@my-node10 conf.d]# cat shop.conf hotel.conf server { listen 80; server_name www.myshop.com; root /application/nginx/shop; index index.html; } server { listen 80; server_name www.myhotel.com; root /application/nginx/hotel; index index.html; }
[root@my-node10 conf.d]# nginx -t -c /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@my-node10 conf.d]# nginx -s reload
访问www.myshop.com 和 www.myhotel.com
192.168.6.102 - - [05/Mar/2023:15:08:59 +0800] "GET / HTTP/1.1" 200 61 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" "-" 192.168.6.102 - - [05/Mar/2023:15:08:59 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://www.myshop.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" "-"
三、Nginx状态监控
--with-http_stub_status_module 记录Nginx客户端访问的状态信息
具体配置:location /mystatus { stub_status on; access_log off; }
- Active connections: The current number of active client connections including Waiting connections. Nginx当前活跃的连接数
- accepts: The total number of accepted client connections.
- handled: The total number of handled connections. requests: The total number of client requests.
- Generally, the parameter value is the same as accepts unless some resource limits have been reached (for example, the worker_connections limit).
- Reading: The current number of connections where nginx is reading the request header.
- Writing: The current number of connections where nginx is writing the response back to the client.
- Waiting: The current number of idle client connections waiting for a request.
四、 虚拟主机
虚拟主机,在Web服务器里是一个独立的网站站点, 站点对应独立的域名(也可以是IP或端口), 具有独立的程序及资源目录,独立对外提供服务。 4.1 基于域名的虚拟主机[root@my-node10 conf.d]# cat hotel.conf server { listen 80; server_name www.myhotel.com; root /application/nginx/hotel; index index.html; } [root@my-node10 conf.d]# cat shop.conf server { listen 80; server_name www.myshop.com; location / { root /application/nginx/shop; index index.html; } }
4.2 配置不同端口访问不同虚拟主机
server { listen 8001; ...... } server { listen 8002; ...... }
4.3 配置不同IP访问不同虚拟主机
server { listen 80; server_name 192.168.6.10; ...... }
4.4 虚拟主机别名
server { listen 80; server_name www.myhotel.com myhotel.com myhoteldev.com; root /application/nginx/hotel; index index.html; }
4.5 文件名的排序
相同的域名, 相同的端口, 谁的文件名在前,谁会被优先读取到。
[root@my-node10 demo]# mkdir d1 d2 d3 [root@my-node10 demo]# echo "d1-1" > d1/index.html [root@my-node10 demo]# echo "d2-1" > d2/index.html [root@my-node10 demo]# echo "d3-1" > d3/index.html [root@my-node10 demo]# echo "demo" > 1.html [root@my-node10 demo]# ls d1 d2 d3 index.html
[root@my-node10 conf.d]# cat demo1.conf server { listen 80; server_name demo.com; root /application/nginx/demo/d1; index index.html index.htm; location ~ ^/1.html { root /application/nginx/demo; } } [root@my-node10 conf.d]# cat demo2.conf server { listen 80; server_name demo.com; root /application/nginx/demo/d2; index index.html index.htm; location ~ ^/1.html { root /application/nginx/demo; } }
[root@my-node10 conf.d]# ll 总用量 28 -rw-r--r-- 1 root root 1152 3月 5 16:21 default.conf -rw-r--r-- 1 root root 227 3月 5 20:03 demo1.conf -rw-r--r-- 1 root root 227 3月 5 20:04 demo2.conf # 访问demo.com, 使用的是 demo1.conf 配置文件 [root@my-node51 ~]# curl http://demo.com d1-1
[root@my-node10 conf.d]# mv demo1.conf demo4.conf [root@my-node10 conf.d]# ll 总用量 28 -rw-r--r-- 1 root root 1152 3月 5 16:21 default.conf -rw-r--r-- 1 root root 227 3月 5 20:04 demo2.conf -rw-r--r-- 1 root root 227 3月 5 20:03 demo4.conf [root@my-node10 conf.d]# nginx -s reload nginx: [warn] conflicting server name "demo.com" on 0.0.0.0:80, ignored [root@my-node51 ~]# curl http://demo.com d2-1 [root@my-node51 ~]# curl http://demo.com/1.html demo
[root@my-node10 conf.d]# cat demo3.conf server { listen 80; server_name demo.com default; root /application/nginx/demo/d3; index index.html index.htm; location ~ ^/1.html { root /application/nginx/demo; } }
[root@my-node51 ~]# curl http://demo.com d2-1 [root@my-node51 ~]# curl http://demo.com/1.html demo
标签:03,nginx,index,配置,Nginx,html,conf,my,root From: https://www.cnblogs.com/kingdomer/p/13936210.html