Frenlee

nginx 二级域名配置

nginx 二级域名的配置,现在一般都喜欢一个站点一个配置文件的配置方式,在nginx.conf 中通过include 的方式加载

nginx配置

修改nginx/conf/nginx.conf 的配置,将server 中的模块移出去,配置到conf.d 文件夹中

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
#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;
include conf.d/*.conf;
}

添加站点配置

创建 nginx/conf/conf.d 文件夹,进入文件夹中创建你需要创建的站点的配置文件,例如 blog.frenlee.com.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
server {
listen 80;
server_name blog.frenlee.com;
root "/path/to/project/root";

index index.php index.html index.htm;

charset utf-8;

location / {
try_files $uri $uri/ /index.php?$query_string;
}


location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

access_log /var/log/nginx/log/host_blog.access.log;
error_log /var/log/nginx/log/host_blog.error.log;

sendfile off;

client_max_body_size 100m;

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}

location ~ /\.ht {
deny all;
}
}

如此就可以了,配置好后,需要nginx -s reload 配置,reload 前可以先检查下配置是否正确 nginx -t,如果需要配置其他的站点,只需要改下域名和根路径就ok!