Running a project from a subpath

Hi, I’ve started running my project by using yii2-app-basic. I’m using nginx as a webserver, and I’m trying to host my project at /site rather than at /.

My nginx configuration is working and showing me the project at http://localhost/site, but all the links generated by Yii point to http://localhost. I’ve also set the ‘baseUrl’ in my request component configuration to ‘/site’.

Anything I’m doing wrong? Is there any way to tell Yii to generate all the links properly?

Hi @yiiuser2019, welcome to the forum!

Can you show us in which directory you have installed your yii2-app-basic? Or, in other words, what is the path to the 'index.php' entry script? For example, I have a yii2-app-basic project installed in '/var/www/diary' (production server) and 'C:\data\diary (development environment), and their paths to the entry script are '/var/www/diary/web/index.php' and 'C:\data\diary\web\index.php' respectively.

And then what do you have for server configuration in your nginx config file? My server config is something like the following:

server {
    charset utf-8;
    client_max_body_size 128M;

    listen 443 ssl http2; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name diary.softark.net;
    root        /var/www/diary/web;
    index       index.php;
    ...

As you see, 'root /var/www/diary/web;' specifies the directory where my index.php is located.

And with this configuration I can access my diary app by the URL of https://diary.softark.net/, not https://diary.softark.net/web/.

Hey, thanks for the quick reply. Here are all my configurations.

I’m running my project from /srv/http/site.

My nginx configuration is

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen 80;
        server_name site.local;

        root /srv/http/site/web;
        index index.php;

        location /site {
                rewrite ^/site/(.*)$ /$1 break;
                try_files $uri $uri/ /index.php?$query_string;
        }

	location ~ ^/site/.*\.php$ {
                rewrite ^/site/(.*)$ /$1 break;
                fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
                include        fastcgi.conf;
        }
    }
}

This setup only works when I open up http://site.local/site/index.php in my browser, http://site.local/site doesn’t work. And as I said, the links that Yii generates all point to site.local and not site.local/site

Also, for transparency, I resolve site.local to 127.0.0.1 in /etc/hosts

Please try the following configuration:

server {
    listen 80;
    server_name site.local;

    root        /srv/http/site/web;
    index       index.php;

    location /site {
        alias /srv/http/site/web;
        try_files $uri $uri/ @site;
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        }
    }

    location @site {
        rewrite /site/(.*)$ /site/index.php?$1 last;
    }
}

This is based on the article of PHP Apps in a Subdirectory in Nginx and is simplified and modified to your environment. I’ve tested it and confirmed that it works fine for me, so I hope it will work for you, too.

Hi, thanks for the help. That doesn’t really work for me,
at http://site.local/site/index.php I get File not found and
at http://site.local/site/ I get “You have choose to open: … which is: application/octet/stream”

Hmm, I don’t know why.
Can you provide us with more information of your configuration?