wojkxlq 发表于 2017-12-22 11:29:14

在nginx上面部署多个项目

  在网上搜了一下,一般有两种方法,第一种方法:在一个配置文件里面操作,把多个域名写在一个配置文件里面,第二种方法:一个域名对应一个配置文件,我是按照第二种方法操作的。比如在一个服务器上面,需要配置的域名为:www.ceshi1.com,www.ceshi2.com,操作步骤为:
  1:在/etc/nginx/下面建立文件夹vhosts
  2:在/etc/nginx/vhosts/文件夹下面,建立文件www.ceshi1.com.conf,www.ceshi2.com.conf
  3:在www.ceshi1.com.conf里面,配置信息:
  server
  {
  listen 80;
  server_nameceshi.banma.com;   ------------》这里写入你自己的域名(www.ceshi1.com)
  index index.html index.htm index.php default.html default.htm default.php;
  root/var/data/www/banma;       --------项目的目录
  #error_page   404   /404.html;
  location / {
  index index.html index.php;
  if (-f $request_filename/index.html){
  rewrite (.*) $1/index.html break;
  }
  if (-f $request_filename/index.php){
  rewrite (.*) $1/index.php;
  }
  if (!-f $request_filename){
  rewrite (.*) /index.php;
  }
  }
  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) {
  denyall;
  }
  #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
  现在介绍一下第一种方法在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
  这就大功告成了!!
页: [1]
查看完整版本: 在nginx上面部署多个项目