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>