|
本次的命令资料全部来自官网除全局定义以及events,地址:https://nginx.org
Nginx 配置组成:
... #全局块
events { #events块
...
}
http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块
}
一、全局定义
########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
二、events定义
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
三、http定义,最为重点
Modules reference下的
1、alias path;
使用场景:location
1
2
3
4
5
6
7
| [iyunv@CentOS7_30 nginx]# grep "download\|alias" nginx.conf #查看配置
location /download/ {
alias /data/site/;
[iyunv@CentOS7_30 nginx]# tail /data/site/index.html #查看site下index.html内容
<h1>Sunshine alias</h1>
[iyunv@CentOS7_30 nginx]# curl http://www.sunshine.com/download/index.html #curl获取信息,获取的是/data/site/index.html内容
<h1>Sunshine alias</h1>
|
2、root path;
使用场景:http, server, location, if in location
1
2
3
4
5
6
7
| [iyunv@CentOS7_30 nginx]# grep "download\|root" nginx.conf #查看配置
location /download/ {
root /data/site;
[iyunv@CentOS7_30 nginx]# cat /data/site/download/index.html #查看download下index.html内容,注意看获取的结果
<h1>Sunshine root</h1>
[iyunv@CentOS7_30 nginx]# curl #更上面alias路径一样,获取到的内容却不一样
<h1>Sunshine alias</h1>
|
#alias与root区别在于,alias把请求/download/变成/data/site/,而root则是把请求的/download/变更/data/site/download/
3、http { ... }
定义:提供服务配置内容,这个没什么好讲的
4、limit_rate rate;
使用场景http, server, location, if in location 定义:现在访问资源的速率
5、limit_except method ... { ... }
使用场景:location 定义:使用的方法
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
| [iyunv@CentOS7_30 nginx]# grep "limit\|allow\|deny" nginx.conf #查看配置
limit_rate 20k;
limit_except GET POST {
#allow 192.168.220.19;
allow 192.168.11.29;
deny all;
[iyunv@CentOS7_30 nginx]# ifconfig | grep "inet 192.168" #查看IP地址
inet 192.168.11.30 netmask 255.255.255.0 broadcast 192.168.11.255
[iyunv@CentOS7_30 nginx]# curl #使用GET获取
<h1>Sunshine root</h1>
[iyunv@CentOS7_30 nginx]# curl -X PUT #使用PUT,报错403
<html
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.8.1</center>
</body>
</html>
[iyunv@CentOS7_29 ~]# ifconfig | grep "inet 192.168" #查看IP地址
inet 192.168.11.29 netmask 255.255.255.0 broadcast 192.168.11.255
[iyunv@CentOS7_29 ~]# curl http://www.sunshine.com/download/index.html #使用GET获取
<h1>Sunshine root</h1>
[iyunv@CentOS7_29 ~]# curl -X PUT #使用PUT,提示405,提示没权
<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.8.1</center>
</body>
</html>
|
6、listen address[:port] listen port listen unix:path
使用场景:server 定义:监听,ip+port port unix:path
7、server {......}
场景:http 定义:虚拟主机
1
2
3
4
| [iyunv@CentOS7_30 nginx]# grep "server\|listen" nginx.conf #查看配置,在http{server}
server {
listen 80;
server_name www.sunshine.com;
|
8、server_name name ...;
场景:server 定义:主机名称
1
2
3
4
| [iyunv@CentOS7_30 nginx]# grep "server\|listen" nginx.conf #查看配置,在server{server_name}
server {
listen 80;
server_name www.sunshine.com;
|
其他的上官网看吧~其实挺简单的。多看看就会了~
|
|
|