1. 程式人生 > >在nginx上面部署多個專案

在nginx上面部署多個專案

在網上搜了一下,一般有兩種方法,第一種方法:在一個配置檔案裡面操作,把多個域名寫在一個配置檔案裡面,第二種方法:一個域名對應一個配置檔案,我是按照第二種方法操作的。比如在一個伺服器上面,需要配置的域名為:www.ceshi1.com,www.ceshi2.com,操作步驟為:

1:在/usr/local/nginx/conf/ 下面建立資料夾vhosts

2:在/usr/local/nginx/conf/vhosts/資料夾下面,建立檔案www.ceshi1.com.conf,www.ceshi2.com.conf

3:在www.ceshi1.com.conf裡面,配置資訊:

server
{
     listen 80;
     server_name ceshi.banma.com;   #------------》這裡寫入你自己的域名(www.ceshi1.com)
     index index.html index.htm index.php default.html default.htm default.php;
     root /usr/local/nginx/html/...;      # --------專案的目錄
     #error_page 404 /404.html;
     location / {
                index index.html index.php;
                if ( !-e $request_filename) {
                        rewrite ^/(.*)$ /index.php/$1 last;
                        break;
                }
       }

     location ~ [^/]\.php(/|$)
     {
               # comment try_files $uri =404; to enable pathinfo
               #try_files $uri =404;
               fastcgi_pass 127.0.0.1:9000;
               fastcgi_index index.php;
               include fastcgi_params;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               fastcgi_split_path_info ^(.+?\.php)(/.*)$;
               set $path_info $fastcgi_path_info;
               fastcgi_param PATH_INFO $path_info;
               try_files $fastcgi_script_name =404; 
               #include fastcgi.conf;
               #include pathinfo.conf;
     }

     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
     {
              expires 30d;
     }

     location ~ .*\.(js|css)?$
     {
              expires 12h;
      }

     # Disallow access to .ht, .svn, .bzr, .git, .hg, .cvs directories
     location ~ /\.(ht|svn|bzr|git|hg|cvs) {
             deny all;
     }

     #access_log /date/nginx/bmp.com.conf/access.log main;

}

4:www.ceshi2.com.conf檔案裡面的內容如上

5:在/etc/nginx/nginx.conf檔案裡面,包含你配置的兩個檔案,加上一句話: include /etc/nginx/vhosts/*;

6:重啟nginx伺服器:service nginx restart

如果重啟失敗,可以這個命令檢視nginx的配置是否正確:nginx -t -c /etc/nginx/nginx.conf

7:在你的本地,hosts檔案裡面加上那兩個域名:伺服器ip www.ceshi1.com,伺服器ip www.ceshi2.com

8:同時配合專案中的 .htaccess 檔案來使用,內容如下:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

如圖:

==============================================================================================

現在介紹一下第一種方法在linux下配置虛擬域名:

在一個配置檔案裡面操作,把多個域名寫在一個配置檔案裡面

比如所有的配置檔案都在/etc/nginx/conf.d目錄下面

開啟一個配置檔案:

然後在主配置檔案裡面:

重啟nginx

重啟nginx的過程中(service nginx restart)出現問題:

可以試一下

 1:先關掉nginx:

nginx -s stop

2:開啟nginx:

nginx -c /etc/nginx/nginx.conf

最後一項在你的host檔案裡面加上這句話:

ip    test.yii.com

這就大功告成了!!