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 {
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;
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 {
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.