nginx安装配置:
配置文件的组成部分:
主配置文件:nginx.conf
include conf.d/*.conf
包含main配置段、event配置段、http配置段、sever配置段。
**main配置段**
正常运行相关的配置;
优化性能相关的配置;
用于调试、定位问题相关的配置;
1、user USERNAME[GROUPNAME];指定用于运行worker进程的用户和组;
2、pid /PATH/TO/PID_FILE;指定nginx进程的pid文件路径;
3、worker_rlimit_nofile #;指定一个worker进程所能够打开的最大文件描述符数量;
4、worker_rlimit_sigpending #;指定每个用户能够发往worker进程的信号的数量;
1、worker_processes #;worker进程的个数;通常应该为物理cpu核心数量减1;
2、worker_cpu_affinity CPUMASK...;指定使用哪个cpu提供服务
3、worker_priority nice;设定优先级;
1、daemon off|on;是否以守护进程的方式启动nginx;
2、master_process on|off;是否以master/worker模型运行nginx;
3、error_log /PATH/TO/ERROR_LOG LEVEL;错误日志文件及其级别;
main配置段简单配置实例
1
2
3
4
| user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
|
**event配置段**
1、worker_connections #;每个worker进程所能够响应的最大并发请求数量;
2、use [epoll|rgsig|select|poll];定义使用的事件模型,建议让nginx自动选择;
3、accept_mutex [on|off];各worker接收用户请求的负载均衡锁;启用时表示让多个worker轮流地、序列化地响应新请求;
4、lock_file /PATH/TO/LOCK_FILE;锁文件
event配置段示例:
1
2
3
4
| events {
use epoll;
worker_connections 1024;
}
|
**http配置段**
1、server{}段配置虚拟主机,作用域:http;
2、location{}可以定义匹配到的URI,即用于需要用到专用配置的URI提供特殊配置;
- 语法: location [=|~|~*|^~] /uri/ { ... } 精确匹配;正则表达式匹配;不区分大小写的正则表达式;左半部分匹配
- 作用域: server
3、listen:
4、server_name:定义主机名称,可以使用通配符和正则表达式;作用域server;
5、root: 语法:root path 作用域:http,server,location
6、alias:路径别名 语法:alias path 作用域:location
区别:
- location /abc/ {
- alias /home/html/abc/;
- }
访问http://172.16.0.1/abc/a.html ==> file /home/html/abc/a.html
- location /abc/ {
- root /home/html/;
- }
访问http://172.16.0.1/abc/a.html ==> file /home/html/abc/a.html
配置实例: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| http {
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; #打开sendfile功能,提供文件传输效率;
tcp_nopush on; #一次性发送所有请求报文首部(仅在sendfile on 时有用)
tcp_nodelay on; #对keepalived模式下是否启用nodelay
keepalive_timeout 65; #客户端保持连接的超时时长
types_hash_max_size 2048;
include /etc/nginx/mime.types; #不同文件后缀对应的MIME
default_type application/octet-stream; #默认MIME类型为二进制字节流
server { #定义虚拟主机
listen 172.16.61.4:80 default_server; #监听的ip及端口
server_name #servername
location / { #定义一个location
index index.html; #定义主页文件
root /data/www/vhost1; #文件目录
}
}
}
|
关于nginx的常用命令:
1
2
3
4
5
| [iyunv@node4 nginx]# nginx -t #检查是否有语法错误
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[iyunv@node4 nginx]# nginx -s reload #重载配置文件
|
|