zoukankan      html  css  js  c++  java
  • nigix 二级域名

    鼓捣了两天服务器,在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行,好了!

    方法很多!

  • 相关阅读:
    在家工作,10招助你效率、生活两不误
    SQL Server智能感知如何更新
    博客园文章《我记录,故我在》读后感
    Python 协程
    Python 用多线程上传和下载文件
    Python 变量交换
    Python 将文件重新命名
    Python ftplib模块
    Python ftplib模块
    Python 函数作为返回值
  • 原文地址:https://www.cnblogs.com/kiscall/p/5092898.html
Copyright © 2011-2022 走看看