多站点配置

当我们有了一个域名,有了一个服务器后,如果我们的服务器只用来运行一个网站之类的程序的话,太浪费了,为了合理的利用其云服务器这个资源,我们可以用它存放和运行多个程序,比如说多个网站

那怎样才能让一个域名,一个服务器运行多个网站呢?

其实就是多站点问题,我们配置一下,就可以了

首先,我的服务器是CentOS6.5,运行LNMP(linux,Nginx,Mysql,php)环境,配置多站点,我们就只需要做两个操作

1.  去域名管理添加解析记录

2.  配置一下nginx的配置文件

我们打开nginx的nginx.conf文件  (一般在 /usr/local/nginx/conf 下)

假设我们有两个子域名:demo1.site.com、demo2.site.com

那我们在nginx.conf下只需这样配置即可

server {
    listen 80;
    server_name demo1.site.com;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default/demo1;
    index index.php index.html index.htm;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }


    location / {
      #try_files $uri $uri/ /index.php?$query_string;
       if (!-e $request_filename){
          rewrite ^/(.*)$ /index.php/$1 last;
     }
    }

    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
      fastcgi_param   PATH_INFO   $fastcgi_path_info;  
      fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ /\.ht {
      deny all;
    }
 }

server {
    listen 80;
    server_name demo2.site.com;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default/demo2;
    index index.php index.html index.htm;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }


    location / {
      #try_files $uri $uri/ /index.php?$query_string;
       if (!-e $request_filename){
          rewrite ^/(.*)$ /index.php/$1 last;
     }
    }

    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
      fastcgi_param   PATH_INFO   $fastcgi_path_info;  
      fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ /\.ht {
      deny all;
    }
 }

当然,通过vhost文件也可以配置,但是各有好处吧,本人比较习惯使用这种方式

重启nginx,就可以看到效果了

猜你喜欢

转载自my.oschina.net/9264736/blog/1813151