YII2部署一个域名多个应用

1 篇文章 0 订阅

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 admin@example.com
  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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的使用Yii2框架实现登录模块的示例代码: 1. 创建一个登录表单模型 LoginForm.php ```php <?php namespace app\models; use Yii; use yii\base\Model; class LoginForm extends Model { public $username; public $password; public $rememberMe = true; private $_user = false; public function rules() { return [ // 验证规则 [['username', 'password'], 'required'], ['rememberMe', 'boolean'], ['password', 'validatePassword'], ]; } public function validatePassword($attribute, $params) { if (!$this->hasErrors()) { $user = $this->getUser(); if (!$user || !$user->validatePassword($this->password)) { $this->addError($attribute, '用户名或密码错误。'); } } } public function login() { if ($this->validate()) { return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); } else { return false; } } public function getUser() { if ($this->_user === false) { $this->_user = User::findByUsername($this->username); } return $this->_user; } } ``` 2. 创建登录页面 views/site/login.php ```php <?php use yii\helpers\Html; use yii\widgets\ActiveForm; $this->title = '登录'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="site-login"> <h1><?= Html::encode($this->title) ?></h1> <p>请填写以下表单进行登录:</p> <?php $form = ActiveForm::begin(['id' => 'login-form']); ?> <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?> <?= $form->field($model, 'password')->passwordInput() ?> <?= $form->field($model, 'rememberMe')->checkbox() ?> <div style="color:#999;margin:1em 0"> 如果你忘记了密码,你可以 <?= Html::a('重置密码', ['site/request-password-reset']) ?>。 </div> <div class="form-group"> <?= Html::submitButton('登录', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?> </div> <?php ActiveForm::end(); ?> </div> ``` 3. 创建控制器 SiteController.php ```php <?php namespace app\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; use app\models\LoginForm; class SiteController extends Controller { public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'only' => ['logout'], 'rules' => [ [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'logout' => ['post'], ], ], ]; } public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ '

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一片红叶88

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值