Nginnx Configuration for Yii 2 Advanced Application Template

I would like to configure the Nginx web server so that:

  • Requests to the URI /index.php must be handled by public_html/frontend/web/index.php
  • Requests to the URI /admin/index.php must be handled by public_html/backend/web/index.php

Please advise where I am wrong. Here is my config:

 server { listen 80; server_name yii2.lo; server_tokens off; client_max_body_size 128M; charset utf-8; access_log /var/log/nginx/yii2-access.log main buffer=50k; error_log /var/log/nginx/yii2-error.log notice; set $host_path "/srv/http/yii2/public"; set $yii_bootstrap "index.php"; index $yii_bootstrap; location / { root $host_path/frontend/web; try_files $uri $uri/ /$yii_bootstrap?$args; } location /admin { root $host_path/backend/web; try_files $uri $uri/ /$yii_bootstrap?$args; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_index $yii_bootstrap; # Connect to php-fpm via socket fastcgi_pass unix:/run/php-fpm/php-fpm.sock; fastcgi_connect_timeout 30s; fastcgi_read_timeout 30s; fastcgi_send_timeout 60s; fastcgi_ignore_client_abort on; fastcgi_pass_header "X-Accel-Expires"; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param HTTP_REFERER $http_referer; include fastcgi_params; } location ~* \.(js|css|less|png|jpg|jpeg|gif|ico|woff|ttf|svg|tpl)$ { expires 24h; access_log off; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; } location ~ /\. { deny all; access_log off; log_not_found off; } } 
+7
source share
4 answers

In short: use the first method below.

The rest of the answer is a list of recommendations.

I am going to separate my answer in two sections. In the first part I will tell you the easiest and fastest way to achieve your goal in accordance with your desired URL requests, but this partially violates the structure of the application, but nothing serious.

In the second part, I will tell you where you made mistakes in your configuration file, and I will show you a poorly written configuration for your needs that works.

I. Deployed Hosting Deployment

I highly recommend you use this. This is the official version from the Yii 2 documentation to make working with the backend in the same domain, although it helps to deploy the project to a shared hosting. And it does not require additional nginx configuration, just basic for the root interface.

Let me write a simple list according to this guide:

  • Move content from /backend/web to /frontend/web/admin .
  • Fix script paths in /frontend/web/admin/index.php (and index-test.php if you use it)

To do this, you have a backend in the same domain at /admin URL. Also, read the last section of the cookie manual. The advanced template was designed to use different domains for each environment, so the guide describes the basic configuration for shared hosting in order to save cookies with the interface and backend separately.

Of course, remember to modify your /environments files to properly initialize your project with the /init script.

II. Nginx configuration

Mistakes

I am not a nginx professional, but I can describe what is wrong with your configuration based on my personal experience and documentation. Unfortunately, I will not be able to provide links to the documentation, because my current rating will not allow me to post more than two links.

root server context

You do not have a root directive in the context of your server. Thus, when matching the location ~ \.php$ it has no root at all and uses nginx root by default. Try installing the general root directive in the server context, then all locations will have it by default. For instance:

 server { # Beginning of your configuration # ... root /srv/http/yii2/public/frontend/web; # The rest of your configuration # ... } 

The absence of a higher contextual root is a common pitfall .

root instead of alias

Secondly, when the location maps, the uri is added to the root of the location and the path that the server is trying to try to find. So your location /admin assumes the server is looking for $host_path/backend/web/admin . In your situation, you should use the alias directive, which tells the server that the agreed location uri refers to the path of aliases, and not to root:

 location /admin { alias $host_path/backend/web; # The rest of location # ... } 

I recommend that you read the related nginx documentation about location , root and alias directives.

Working but poorly written configuration

I post this sample configuration with comments only for your understanding, and not for use in production, I suggest you use it for your production (until you put it on a safe and sound one).

It works, but has an unpleasant defect: the backend cannot find the Yii2 script entry if you request it directly (for example, /admin/index.php ), therefore it must be used with enablePrettyUrl set to true and showScriptName set to false , however it finds any other PHP script in the root web root.

 server { # The beginning of your configuration # ... # By default we will provide frontend root /srv/http/yii2/public/frontend/web; index index.php; location / { try_files $uri $uri/ /index.php?$args; } location /admin { # We use /web/index here to make backend call to PHP s # distinct from frontend call index /web/index.php; alias $root_base/backend/web; try_files $uri $uri/ /web/index.php?$args; # Rewrite PHP requests from /admin to /web # However, Yii2 entry script returns 404 location ~ ^/admin/.*\.php$ { rewrite ^/admin/(.*)$ /web/$1; } } location ~ ^/web/.*\.php$ { # Make sure this location cannot be called externally internal; # Remember, that the uri of this location # will be appended to this root! root $root_base/backend; # PHP settings for backend } location ~ \.php$ { # PHP settings for frontend } # The rest of your configuration # ... } 

Also, add the baseUrl property to the request component in your Yii2 backend configuration and set it to /admin .

I hope my answer will help you deploy your advanced Yii2 project and understand nginx more, however your question is 6 months.

+15
source

Here is my working configuration based on the accepted answer. My backend project has been renamed to admin

 # Example config for nginx # frontend is available on yii-application.local/ # backend (admin) is available on yii-application.local/admin # make sure that @app/frontend/config/main.php and @app/admin/config/main.php components sections are configured properly # eg @app/frontend/config/main.php # 'homeUrl' => '', # ... # 'components' => [ # 'request' => [ # 'baseUrl' => '', # ], # 'urlManager' => [ # 'enablePrettyUrl' => true, # 'showScriptName' => false, # ], # ] # # eg @app/admin/config/main.php # 'homeUrl' => '/admin', # ... # 'components => [ # 'request' => [ # 'baseUrl' => '/admin', # ], # 'urlManager' => [ # 'enablePrettyUrl' => true, # 'showScriptName' => false, # ], # ] server { set $project_root /home/yii/apps/yii-advanced; set $fcgi_server unix:/opt/php/var/run/php5-fpm.sock; charset utf-8; client_max_body_size 128M; listen 80; server_name yii-application.local; root $project_root/frontend/web; index index.php; access_log /home/yii/apps/yii-advanced/logs/access-backend.log; error_log /home/yii/apps/yii-advanced/logs/error-backend.log; location / { try_files $uri $uri/ /index.php?$args; } location /admin { index /web/index.php; alias $project_root/admin/web; try_files $uri $uri/ /web/index.php?$args; location ~ ^/admin/.*\.php$ { rewrite ^/admin/(.*)$ /web/$1; fastcgi_pass $fcgi_server; include fastcgi.conf; } } location ~ ^/web/.*\.php$ { internal; root $project_root/admin; fastcgi_pass $fcgi_server; include fastcgi.conf; } location ~* \.php$ { try_files $uri =404; fastcgi_pass $fcgi_server; include fastcgi.conf; } location ~* \.(htaccess|htpasswd|svn|git) { deny all; } } 
+2
source

Try to specify the Nginx configuration: I use the configuration for the "Advanced" template in the domain configuration file, specify the interface like this:

listen frontend.site.loc: 80; # for the interface

specify the backend domain: listen backend.site.loc: 80; # for backend

0
source

This guy did a very good job of creating an advanced nginx application configuration (with a subdomain which IMHO is the best installation): https://gist.github.com/Kison/45ec9ce3c1ebf422cbd42bd5ce04d8e4

0
source

Source: https://habr.com/ru/post/971801/


All Articles