鼓捣了两天服务器,在digitalocean上买的,挺好用,性价比也高。
弄了VPN,shadowsockets,OpenVPN,我本来以为VPN可以提供网络接入的,实在是图样图森破了。
搭服务器使用的lnmp,nginx是第一次玩,有很多坑,这个废了好大得劲才搞好。
这篇主要写nginx的一二级域名跳转问题。
我申请了kiscall.top域名,这里写domain.com。
默认配置后访问domain.com会访问/usr/share/nginx/html目录,改的方法很简单:
vim /etc/nginx/sites-available/default
修改root:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; # 就是这一行 root /usr/share/nginx/html; index index.html index.htm; }
这样就能实现访问domain.com时访问自己想访问的目录了。
但是我想实现这个功能,
www.domain.com -> /Sites/www目录
api.domain.com -> /Sites/api目录
test.domain.com -> /Sites/test目录
这个时候要这么配,注意3、4行的正则表达式匹配,在第三行匹配后第四行使用。
1 server { 2 listen 80; 3 server_name ~^(?<subdomain>.+).kiscall.top$; 4 root /Sites/$subdomain; 5 index index.html index.htm; 6 7 server_name localhost; 8 9 location / { 10 try_files $uri $uri/ =404; 11 } 12 location ~ .php$ { 13 try_files $uri = 404; 14 fastcgi_split_path_info ^(.+.php)(/.+)$; 15 fastcgi_pass unix:/var/run/php5-fpm.sock; 16 fastcgi_index index.php; 17 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 18 include fastcgi_params; 19 } 20 21 }
成功了!
但是又有一个问题,虽然三个二级域名配置好了,但是domain.com跪了,怎么破?
我是这样解决的,在写一个server,所以配置文件变成了这样:
1 server { 2 listen 80; 3 server_name ~^(?<subdomain>.+).kiscall.top$; 4 root /Sites/$subdomain; 5 index index.html index.htm; 6 7 server_name localhost; 8 9 location / { 10 try_files $uri $uri/ =404; 11 } 12 location ~ .php$ { 13 try_files $uri = 404; 14 fastcgi_split_path_info ^(.+.php)(/.+)$; 15 fastcgi_pass unix:/var/run/php5-fpm.sock; 16 fastcgi_index index.php; 17 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 18 include fastcgi_params; 19 } 20 21 } 22 23 server { 24 listen 80; 25 server_name kiscall.top; 26 root /Sites/Index; 27 index index.html index.htm; 28 29 server_name localhost; 30 31 location / { 32 try_files $uri $uri/ =404; 33 } 34 location ~ .php$ { 35 try_files $uri = 404; 36 fastcgi_split_path_info ^(.+.php)(/.+)$; 37 fastcgi_pass unix:/var/run/php5-fpm.sock; 38 fastcgi_index index.php; 39 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 40 include fastcgi_params; 41 } 42 43 }
重点看第二个server,就是第25、26行,好了!
方法很多!