A
A
Anton2014-06-09 15:44:05
Nginx
Anton, 2014-06-09 15:44:05

How to set up a Yii2 site in Nginx to work through location?

The question is the following. There is one domain (www.domain.com) and several Yii2 web applications. You need to configure Nginx so that all web applications open through a location, for example www.domain.com/site1 and at the same time url rewrite works. The whole thing is spinning on Debian 7.0.5. Subdomains for each site in my case are not suitable - the domain is only www.
My config:

server {
  charset utf-8;
  client_max_body_size 128M;

  listen 80;
  root /var/www;
  index index.php;

  error_log /var/log/nginx/error.log debug;

  location /site1/ {
    alias /var/www/site1/web/;
    try_files $uri $uri/ /index.php?$args;

    location ~ ^/site1/(.+\.php)$ {
      include yii.conf;
      fastcgi_param SCRIPT_FILENAME /var/www/test/web/index.php;
    }
  }

  location ~ /\.(ht|svn|git) {
    deny all;
  }
}

yii.conf file:
fastcgi_split_path_info ^(.+\.php)(/.+)$;

fastcgi_hide_header "X-Powered-CMS";
fastcgi_hide_header "X-Powered-By";

fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vyachin, 2015-04-07
@vyachin

For each application, make your own server section as written here www.yiiframework.com/doc-2.0/guide-start-installat... but specify your UNIQUE PORT NUMBER listen 8081; different from 80 (you can even screw a unix socket, it seems to me that it will work even faster). In the main server section, do everything through location with proxying to a specific section for the application you need.

server {
    server_name www.domain.com;
    location /site1 {
        proxy_pass http://127.0.0.1:8081;
    }
    location /site2 {
        proxy_pass http://127.0.0.1:8082;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question