Nginx基础

基础命令

  • nginx -s stop - 快速关机(不管有没有正在处理的请求,Nginx退出)
  • nginx -s quit - 优雅的关机(Nginx在退出前完成已经接受的连接请求)
  • nginx -s reload - 重新加载配置文件
  • nginx -s reopen - 重新打开日志文件
  • ps -ef | grep nginx - 获取所有正在运行的nginx进程的列表
  • nginx -t - 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
  • nginx -c </path/to/config> 为 Nginx 指定一个配置文件,来代替缺省的

nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#配置用户或者组
#user nobody;
#启动进程,设置成与cpu数量相等
worker_processes 1;

#全局错误日志及PID
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

#工作模式及连接数上限
events {
#设置网路连接序列化,防止惊群现象发生,默认为on
#惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。
accept_mutex on;
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,
#仅用于linux2.6以上内核,可以大大提高nginx的性能
#事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
#use epoll;

#单个后台worker process进程的最大并发链接数
worker_connections 1024;


}


http {
#mime.types是用来定义,请求返回的content-type
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"';

#access_log logs/access.log main;

#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
#对于普通应用,必须设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
#以平衡磁盘与网络I/O处理速度,降低系统的uptime.
# 打开sendfile,用户请求的数据不用再加载到nginx的内存中,而是直接发送,减少了一次消耗
sendfile on;
#tcp_nopush on;

#接连超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#默认开启,作用就是禁用 Nagle’s Algorithm
tcp_nodelay on;

#开启gzip压缩
gzip on;
#IE6的某些版本对gzip的压缩支持很不好,会造成页面的假死,所以禁用IE6的gzip压缩
gzip_disable "MSIE [1-6].";

#设定请求缓冲
#nginx默认的header长度上限是4k,如果超过了这个值,如果header头信息请求超过了,nginx会直接返回400错误
#如果请求中的header都很大,那么使用client_header_buffer_size可以减少一次内存分配
client_header_buffer_size 128k;
#如果你的请求中只有少量请求header很大,那么应该使用large_client_header_buffers,因为这样就仅需在处理大header时才会分配更多的空间,从而减少无谓的内存空间浪费
large_client_header_buffers 4 128k;

#设定虚拟主机配置,每个server互不干扰
server {
#侦听7070端口
listen 7070;
#定义使用localhost访问
server_name localhost;

#定义服务器的默认网站根目录位置
root html;

#charset koi8-r;

#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;

#默认请求
location / {
#定义首页索引文件的名称
root /Users/mintaoyu/myhexo/public/;
#拒绝的ip
#deny 127.0.0.1;
#允许的ip
#allow 172.18.5.54;
}


#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 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

# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
#\{
# 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 servers/*;
}

关于NG配置中root和alias的区别

1
2
3
4
5
6
7
8
9
10
# alias配置下ng最终访问的路径是 alias(即/home/www/huan/)
# alias配置的路径最后不能忘了加‘/’
location /huan {
alias /home/www/huan/;
}
# root配置下ng最终访问的路径是 root+ location(即/huan/home/www)
# 静态页面必须是放在huan目录下的
location /huan {
root /home/www;
}
赏个🍗吧
0%