YII2部署一个域名多个应用

Yii2高级模板,一个域名部署多个应用

YIi2框架开发,的确用起来使用方便,但是在实际中需要一个域名绑定多个应用,在现在开发模式下,前后端一般前后端分离,web开发也使用了大前端模式,采用api + vue等开发模式,就遇到了新的问题,一个域名怎么部署多个应用呢,这里就需要用了nginx的正则了。这里采用两种方式实现,1、纯粹使用nginx配置多个项目;2、使用nginx + apache 结合,nginx处理静态内容,apache处理php,通过nginx代理方式转发。下面直接上代码:

1.纯粹nginx方式

server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/app/frontend/web;
    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #静态文件转发,例如上传的图片到static/upload/image目录
    location ^~ /static {
	root /data/wwwroot/marathon;
    } 
    #后台采用混编+部分模块vue方式,
    location ^~ /admin/vue/ {
        alias /data/wwwroot/app-vue/dist/;
	    index index.html index.htm;
    }
    
    #admin后台
    location ^~ /admin/ {
        alias /data/wwwroot/app/backend/web/;
        if ($uri ~ "^/admin/$"){
                rewrite ^/admin/(.*) /admin/index.php?r=$1 last;
        }

        if (!-e $request_filename) {
            rewrite ^/admin/(.*) /admin/index.php?r=$1 last;
        }
         location ~ [^/]\.php(/|$) {
           #fastcgi_pass remote_php_ip:9000;
           fastcgi_pass unix:/dev/shm/php-cgi.sock;
           fastcgi_index index.php;
           include fastcgi.conf;

           fastcgi_split_path_info ^(.+\.php)(/.+)$;
           fastcgi_param   PATH_INFO   $fastcgi_path_info;
           fastcgi_param   SCRIPT_FILENAME /data/wwwroot/app/backend/web/index.php;
        }
    }
    #
    location / {
	if (!-e $request_filename) {
            rewrite ^/(.*) /index.php?r=$1 last;
        }
    }
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
  }


2.ngin + apache

server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/marathon;
    index index.html index.htm index.php;
 
   #nginx 处理静态文件,上传的文件static/upload/image
    location ^~ /static {/
	       root /data/wwwroot/app;
    }
    
    #后台混编模块 + vue模块
     location ^~  /admin/vue/ {
		alias /data/wwwroot/app/backend/static_shangma/ShangMa/dist/;
		index index.html index.htm;
   }    

    #admin 混编模块代理到Apache下面处理
    location ^~ /admin/ {
       #apache网站的ip地址,这里写成127.0.0.1
      	proxy_pass http://127.0.0.1:88/;
	    proxy_http_version 1.1;
        proxy_set_header Connection "";

        proxy_set_header Host $proxy_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $http_x_forwarded_for;
        #proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

   }

   #home 代理到apache下处理
   location /{
	#apache网站的ip地址,这里写成127.0.0.1
	proxy_pass http://127.0.0.1:8088/;
	proxy_http_version 1.1;
	proxy_set_header Connection "";

	proxy_set_header Host $proxy_host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-Server $host;
	proxy_set_header X-Forwarded-For $http_x_forwarded_for;
	#proxy_set_header X-Forwarded-For $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }

 }

3、apache 配置

<VirtualHost *:88>
  ServerAdmin [email protected]
  DocumentRoot "/data/wwwroot/app/backend/web"
  ServerName 127.0.0.1
  ErrorLog "/data/wwwlogs/error_apache.log"
  CustomLog "/data/wwwlogs/access_apache.log" common
  <Files ~ (\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)$>
    Order allow,deny
    Deny from all
  </Files>
  <Files ~ (\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)$>
    Order allow,deny
    Deny from all
  </Files>
  <FilesMatch \.php$>
    SetHandler "proxy:unix:/dev/shm/php-cgi.sock|fcgi://localhost"
  </FilesMatch>
<Directory "/data/wwwroot/app/backend/web">
  SetOutputFilter DEFLATE
  Options FollowSymLinks ExecCGI
  Require all granted
  AllowOverride All
  Order allow,deny
  Allow from all
  DirectoryIndex index.html index.php
</Directory>
<Location /server-status>
  SetHandler server-status
  Order Deny,Allow
  Deny from all
  Allow from 127.0.0.1
</Location>
</VirtualHost>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章