Skip to content

Commit 210eb2d

Browse files
committed
docs folder path page
1 parent 102013e commit 210eb2d

22 files changed

+4520
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
#### Nginx服务器基础配置命令
3+
---
4+
+ 基于域名的虚拟主机配置
5+
+ 语法格式:
6+
```
7+
server_name name name2 name3 ... # 可以有一个或者多个名称并列,之间用空格隔开
8+
```
9+
+ 普通案例:
10+
```
11+
server_name www.tinywan.com www.redis.com # 第一个名称作为虚拟主机的主要名称
12+
```
13+
+ 通配符案例:
14+
```
15+
server_name *.tinywan.com www.redis.* # 通配符只能用在域名字符串名称的手段或尾端
16+
```
17+
+ 正则匹配案例:
18+
```
19+
server_name ~^www\d+\.tinywan.com$
20+
```
21+
> 波浪号 ` ~ ` 作为正则表达式字符串的开始标记
22+
> 正则表达式含义:
23+
>>` ^www ` 以 www 开头
24+
>>` \d+ ` ,`\d` 代表 0 ~ 9 的某一个数字,`+` 表示以前的字符出现一次或者多次
25+
>>`\.` ,由于`.`在正则表达式有特殊含义,因此需要转义字符`\`进行转义
26+
>> ` m$` 表示一个m结束
27+
28+
+ 访问服务器域名
29+
> 可以访问
30+
```
31+
www1.tinywan.com
32+
```
33+
34+
> 不可以访问
35+
```
36+
www.tinywan.com
37+
```
38+
+ 基于IP的虚拟主机配置
39+
+ 本机IP地址为: `192.168.127.129`
40+
+ 添加 IP 别名 (`192.168.127.131` 和 `192.168.127.141`)
41+
```
42+
sudo ifconfig ens33:0 192.168.127.131 netmask 255.255.255.0 up
43+
44+
sudo ifconfig ens33:1 192.168.127.141 netmask 255.255.255.0 up
45+
```
46+
+ 将以上两条命令添加到Linux 系统的启动脚本rc.local 中,系统重启后,ens33 的别名就自动设置好了(注意:sudo 权限)
47+
```
48+
echo "ifconfig ens33:1 192.168.127.131 netmask 255.255.255.0 up" >> /etc/rc.local
49+
50+
echo "ifconfig ens33:1 192.168.127.131 netmask 255.255.255.0 up" >> /etc/rc.local
51+
```
52+
+ /etc/rc.local 解释
53+
>在Linux启动的最后阶段,系统会执行存于rc.local中的命令
54+
55+
+ 配置虚拟主机
56+
```
57+
server {
58+
listen 80;
59+
server_name 192.168.127.131;
60+
location / {
61+
root /usr/local/nginx/html2;
62+
}
63+
}
64+
65+
server {
66+
listen 80;
67+
server_name 192.168.127.141;
68+
location / {
69+
root /usr/local/nginx/html3;
70+
}
71+
}
72+
```
73+
74+
75+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
2+
#### 基础配置文件
3+
---
4+
+ 完整基础配置nginx.conf
5+
```
6+
user www www; ## Default: nobody
7+
worker_processes 5; ## Default: 1
8+
error_log logs/error.log;
9+
pid logs/nginx.pid;
10+
worker_rlimit_nofile 8192;
11+
12+
events {
13+
worker_connections 4096; ## Default: 1024
14+
}
15+
16+
http {
17+
include conf/mime.types;
18+
include /etc/nginx/proxy.conf;
19+
include /etc/nginx/fastcgi.conf;
20+
index index.html index.htm index.php;
21+
22+
default_type application/octet-stream;
23+
log_format main '$remote_addr - $remote_user [$time_local] $status '
24+
'"$request" $body_bytes_sent "$http_referer" '
25+
'"$http_user_agent" "$http_x_forwarded_for"';
26+
access_log logs/access.log main;
27+
sendfile on;
28+
tcp_nopush on;
29+
server_names_hash_bucket_size 128; # this seems to be required for some vhosts
30+
31+
server { # php/fastcgi
32+
listen 80;
33+
server_name domain1.com www.domain1.com;
34+
access_log logs/domain1.access.log main;
35+
root html;
36+
37+
location ~ \.php$ {
38+
fastcgi_pass 127.0.0.1:1025;
39+
}
40+
}
41+
42+
server { # simple reverse-proxy
43+
listen 80;
44+
server_name domain2.com www.domain2.com;
45+
access_log logs/domain2.access.log main;
46+
47+
# serve static files
48+
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
49+
root /var/www/virtual/big.server.com/htdocs;
50+
expires 30d;
51+
}
52+
53+
# pass requests for dynamic content to rails/turbogears/zope, et al
54+
location / {
55+
proxy_pass http://127.0.0.1:8080;
56+
}
57+
}
58+
59+
upstream big_server_com {
60+
server 127.0.0.3:8000 weight=5;
61+
server 127.0.0.3:8001 weight=5;
62+
server 192.168.0.1:8000;
63+
server 192.168.0.1:8001;
64+
}
65+
66+
server { # simple load balancing
67+
listen 80;
68+
server_name big.server.com;
69+
access_log logs/big.server.access.log main;
70+
71+
location / {
72+
proxy_pass http://big_server_com;
73+
}
74+
}
75+
}
76+
```
77+
+ proxy_conf 扩展参数
78+
```
79+
proxy_redirect off;
80+
proxy_set_header Host $host;
81+
proxy_set_header X-Real-IP $remote_addr;
82+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
83+
client_max_body_size 10m;
84+
client_body_buffer_size 128k;
85+
proxy_connect_timeout 90;
86+
proxy_send_timeout 90;
87+
proxy_read_timeout 90;
88+
proxy_buffers 32 4k;
89+
```
90+
+ fastcgi_conf 扩展参数
91+
```
92+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
93+
fastcgi_param QUERY_STRING $query_string;
94+
fastcgi_param REQUEST_METHOD $request_method;
95+
fastcgi_param CONTENT_TYPE $content_type;
96+
fastcgi_param CONTENT_LENGTH $content_length;
97+
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
98+
fastcgi_param REQUEST_URI $request_uri;
99+
fastcgi_param DOCUMENT_URI $document_uri;
100+
fastcgi_param DOCUMENT_ROOT $document_root;
101+
fastcgi_param SERVER_PROTOCOL $server_protocol;
102+
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
103+
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
104+
fastcgi_param REMOTE_ADDR $remote_addr;
105+
fastcgi_param REMOTE_PORT $remote_port;
106+
fastcgi_param SERVER_ADDR $server_addr;
107+
fastcgi_param SERVER_PORT $server_port;
108+
fastcgi_param SERVER_NAME $server_name;
109+
110+
fastcgi_index index.php;
111+
112+
fastcgi_param REDIRECT_STATUS 200; 32 4k;
113+
```
114+
+ mime_types 扩展参数
115+
```
116+
types {
117+
text/html html htm shtml;
118+
text/css css;
119+
text/xml xml rss;
120+
image/gif gif;
121+
image/jpeg jpeg jpg;
122+
application/x-javascript js;
123+
text/plain txt;
124+
text/x-component htc;
125+
text/mathml mml;
126+
image/png png;
127+
image/x-icon ico;
128+
image/x-jng jng;
129+
image/vnd.wap.wbmp wbmp;
130+
application/java-archive jar war ear;
131+
application/mac-binhex40 hqx;
132+
application/pdf pdf;
133+
application/x-cocoa cco;
134+
application/x-java-archive-diff jardiff;
135+
application/x-java-jnlp-file jnlp;
136+
application/x-makeself run;
137+
application/x-perl pl pm;
138+
application/x-pilot prc pdb;
139+
application/x-rar-compressed rar;
140+
application/x-redhat-package-manager rpm;
141+
application/x-sea sea;
142+
application/x-shockwave-flash swf;
143+
application/x-stuffit sit;
144+
application/x-tcl tcl tk;
145+
application/x-x509-ca-cert der pem crt;
146+
application/x-xpinstall xpi;
147+
application/zip zip;
148+
application/octet-stream deb;
149+
application/octet-stream bin exe dll;
150+
application/octet-stream dmg;
151+
application/octet-stream eot;
152+
application/octet-stream iso img;
153+
application/octet-stream msi msp msm;
154+
audio/mpeg mp3;
155+
audio/x-realaudio ra;
156+
video/mpeg mpeg mpg;
157+
video/quicktime mov;
158+
video/x-flv flv;
159+
video/x-msvideo avi;
160+
video/x-ms-wmv wmv;
161+
video/x-ms-asf asx asf;
162+
video/x-mng mng;
163+
}
164+
```
165+
+ 生产环境的完整配置nginx.conf
166+
```
167+
user www www;
168+
worker_processes 2;
169+
pid /var/run/nginx.pid;
170+
171+
# [ debug | info | notice | warn | error | crit ]
172+
error_log /var/log/nginx.error_log info;
173+
174+
events {
175+
worker_connections 2000;
176+
# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;
177+
use kqueue;
178+
}
179+
180+
http {
181+
include conf/mime.types;
182+
default_type application/octet-stream;
183+
184+
log_format main '$remote_addr - $remote_user [$time_local] '
185+
'"$request" $status $bytes_sent '
186+
'"$http_referer" "$http_user_agent" '
187+
'"$gzip_ratio"';
188+
189+
log_format download '$remote_addr - $remote_user [$time_local] '
190+
'"$request" $status $bytes_sent '
191+
'"$http_referer" "$http_user_agent" '
192+
'"$http_range" "$sent_http_content_range"';
193+
194+
client_header_timeout 3m;
195+
client_body_timeout 3m;
196+
send_timeout 3m;
197+
198+
client_header_buffer_size 1k;
199+
large_client_header_buffers 4 4k;
200+
201+
gzip on;
202+
gzip_min_length 1100;
203+
gzip_buffers 4 8k;
204+
gzip_types text/plain;
205+
206+
output_buffers 1 32k;
207+
postpone_output 1460;
208+
209+
sendfile on;
210+
tcp_nopush on;
211+
212+
tcp_nodelay on;
213+
send_lowat 12000;
214+
215+
keepalive_timeout 75 20;
216+
217+
# lingering_time 30;
218+
# lingering_timeout 10;
219+
# reset_timedout_connection on;
220+
221+
222+
server {
223+
listen one.example.com;
224+
server_name one.example.com www.one.example.com;
225+
226+
access_log /var/log/nginx.access_log main;
227+
228+
location / {
229+
proxy_pass http://127.0.0.1/;
230+
proxy_redirect off;
231+
232+
proxy_set_header Host $host;
233+
proxy_set_header X-Real-IP $remote_addr;
234+
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
235+
236+
client_max_body_size 10m;
237+
client_body_buffer_size 128k;
238+
239+
client_body_temp_path /var/nginx/client_body_temp;
240+
241+
proxy_connect_timeout 90;
242+
proxy_send_timeout 90;
243+
proxy_read_timeout 90;
244+
proxy_send_lowat 12000;
245+
246+
proxy_buffer_size 4k;
247+
proxy_buffers 4 32k;
248+
proxy_busy_buffers_size 64k;
249+
proxy_temp_file_write_size 64k;
250+
251+
proxy_temp_path /var/nginx/proxy_temp;
252+
253+
charset koi8-r;
254+
}
255+
256+
error_page 404 /404.html;
257+
258+
location /404.html {
259+
root /spool/www;
260+
261+
charset on;
262+
source_charset koi8-r;
263+
}
264+
265+
location /old_stuff/ {
266+
rewrite ^/old_stuff/(.*)$ /new_stuff/$1 permanent;
267+
}
268+
269+
location /download/ {
270+
valid_referers none blocked server_names *.example.com;
271+
272+
if ($invalid_referer) {
273+
#rewrite ^/ http://www.example.com/;
274+
return 403;
275+
}
276+
277+
# rewrite_log on;
278+
# rewrite /download/*/mp3/*.any_ext to /download/*/mp3/*.mp3
279+
rewrite ^/(download/.*)/mp3/(.*)\..*$ /$1/mp3/$2.mp3 break;
280+
281+
root /spool/www;
282+
# autoindex on;
283+
access_log /var/log/nginx-download.access_log download;
284+
}
285+
286+
location ~* ^.+\.(jpg|jpeg|gif)$ {
287+
root /spool/www;
288+
access_log off;
289+
expires 30d;
290+
}
291+
292+
location ~ \.php$ {
293+
fastcgi_pass unix:/var/run/php7.0.9-fpm.sock;
294+
fastcgi_index index.php;
295+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
296+
include fastcgi_params;
297+
}
298+
299+
}
300+
}
301+
```
302+

0 commit comments

Comments
 (0)