Nginx配置基本说明
以下是nginx的基本配置文件如下(编辑命令:vi /usr/local/nginx/conf/nginx.conf):
1 #user nobody;
2 #nginx进程数,建议设置为等于CPU总核心数。
3 worker_processes 1;
4
5 #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
6 #error_log logs/error.log;
7 #error_log logs/error.log notice;
8 #error_log logs/error.log info;
9
10 #进程pid文件
11 #pid logs/nginx.pid;
12
13
14 events {
15 #单个进程最大连接数(最大连接数=连接数*进程数),一个请求的连接一般是2(静态)和4(代理)
16 worker_connections 1024;
17 }
18
19 #设定http服务器,利用它的反向代理功能提供负载均衡支持
20 http {
21 #文件扩展名与文件类型映射表
22 include mime.types;
23 #默认文件类型
24 default_type application/octet-stream;
25
26 #日志格式设定
27 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
28 # '$status $body_bytes_sent "$http_referer" '
29 # '"$http_user_agent" "$http_x_forwarded_for"';
30
31 #access_log logs/access.log main;
32
33 #开启高效文件传输模式
34 sendfile on;
35 #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
36 #tcp_nopush on;
37
38 #长连接超时时间,单位是秒
39 #keepalive_timeout 0;
40 keepalive_timeout 65;
41
42 #gzip模块设置
43 #gzip on;
44
45 #虚拟主机的配置
46 server {
47 #监听端口
48 listen 80;
49 #域名可以有多个,用空格隔开
50 server_name localhost;
51
52 #charset koi8-r;
53
54 #定义本虚拟主机的访问日志
55 #access_log logs/host.access.log main;
56 #代理位置
57 location / {
58 root html; //根目录
59 index index.html index.htm; //首页
60
61 }
62
63 #错误页
64 #error_page 404 /404.html;
65 #重定向错误页
66 # redirect server error pages to the static page /50x.html
67 #
68 error_page 500 502 503 504 /50x.html;
69 location = /50x.html {
70 root html;
71 }
72
73
74
75 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
76 #
77 #location ~ \.php$ {
78 # proxy_pass http://127.0.0.1;
79 #}
80
81 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
82 #
83 #location ~ \.php$ {
84 # root html;
85 # fastcgi_pass 127.0.0.1:9000;
86 # fastcgi_index index.php;
87 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
88 # include fastcgi_params;
89 #}
90
91 # deny access to .htaccess files, if Apache's document root
92 # concurs with nginx's one
93 #
94 #location ~ /\.ht {
95 # deny all;
96 #}
97 }
98
99
100 # another virtual host using mix of IP-, name-, and port-based configuration
101 #
102 #server {
103 # listen 8000;
104 # listen somename:8080;
105 # server_name somename alias another.alias;
106
107 # location / {
108 # root html;
109 # index index.html index.htm;
110 # }
111 #}
112
113
114 # HTTPS server
115 #
116 #server {
117 # listen 443 ssl;
118 # server_name localhost;
119
120 # ssl_certificate cert.pem;
121 # ssl_certificate_key cert.key;
122
123 # ssl_session_cache shared:SSL:1m;
124 # ssl_session_timeout 5m;
125
126 # ssl_ciphers HIGH:!aNULL:!MD5;
127 # ssl_prefer_server_ciphers on;
128
129 # location / {
130 # root html;
131 # index index.html index.htm;
132 # }
133 #}
134
135 }
136
137
检查配置文件是否正确命令:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
Location配置规则
location
语法:location [=|~|~*|^~] /uri/{...}
使用范围:server中使用
这个参数根据URI的不同需求进行配置,可以使用字符串与正则表达式匹配,如果要使用正则表达式,你必须制定下列前缀:
- ~:区分大小写
- ~*:不区分大小写
- ^*:禁止表达式匹配
- =:精确匹配
例子如下:
1 location = / {
2 #只匹配/的查询
3 [configuration A]
4 }
5
6 location / {
7 #匹配任何以/开始的查询,但是正则表达式与一些长的字符串将被首先匹配
8 [configuration B]
9 }
10
11 location ^- /images/ {
12 #匹配任何以/images/开始的查询并且停止搜索,不检查正则表达式
13 [configuration C]
14 }
15
16 location ~* \.(gif|jpg|png)$ {
17 #匹配任何以gif|jpg|png结尾的文件,但是所有/images/目录的请求在configuration C处理
18 [configuration D]
19 }
20
21 各请求的计算如下:
22 ./ -> configuration A
23 ./documents/document.html -> configuration B
24 ./images/1.gif -> configuration C
25 ./documents/1.jpg -> configuration D
View Code
其他功能配置
配置访问日志及错误日历
- 去掉配置文件前端的日志文件格式注释
- 在server模块中,设置访问日志地址,以及错误日志地址
- 重启nginx,并访问server即可看到相应的日志文件中有日志。
配置错误页面
- 在server模块中,设置error_page,并且可以指定错误页面的根目录
- 重启nginx,并访问server报错,即可看到制定的错误界面。
配置自动索引及别名功能
- 在server模块中,加入一个location,开启自动索引,然后在更目录下新建文件data和其他文件,如下:
在浏览器中输入http://server/data,进行访问,如下: - 在server模块中,进入一个location,开启别名功能,如下:
在浏览器中输入http://server/b,进行访问,请求实际访问的地址是/usr/local/nginx/html2。
配置文件浏览器缓存
- 设置图片缓存时间为1天
配置下载限速
- nginx可对下载文件进行限制,在location中加入参数:
配置访问控制及身份验证
- 在location中加入参数如下:
- 配置密码文件,在/usr/local/nginx目录下新建文件.htpasswd文件,并且使用命令添加用户名和密码,命令:printf "test:$(openssl passwd -crypt 123456)\n" >>/usr/local/nginx/.htpasswd
其中用户名是:test,密码是:123456 - 在浏览器中进行访问,需要密码进行登陆。如下
配置htts代理
- 需要nginx支持ssl模块
即编译nginx加入此模块(--with-http_ssl_module),命令:./configure --prefix=/data/soft/nginx --with-http_ssl_module - 配置文件配置如下
1 location /https {
2 proxy_http_version 1.1;
3 proxy_pass https://www.baidu.com;
4 proxy_set_header X-B3-TraceId $request_id;
5 proxy_set_header X-Span-Name $uri;
6 proxy_set_header Host www.baidu.com;
7 proxy_set_header X-Real-IP $remote_addr;
8 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9 }
- 重启nginx,即可代理https的请求