0

I have a problem to define proper vhost rules for nginx hosting 2 separate applications. Unfortunately, I can't use subdomain on the host, so I'm stuck with a subfolder approach. The main application is the TYPO3, with the following vhost config:


server {
    listen 80 default_server;
    listen [::]:80 default_server; 

    root /var/www/typo3dev/web;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location = /favicon.ico {
                log_not_found off;
                access_log off;
       }

       location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
       }


       # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
       location ~ /\. {
                deny all;
                access_log off;
                log_not_found off;
       }

    location ~ \.php$ {
                        try_files $uri =404;
                        include /etc/nginx/fastcgi_params;
                        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_intercept_errors on;
                        fastcgi_buffer_size 128k;
                        fastcgi_buffers 256 16k;
                        fastcgi_busy_buffers_size 256k;
                        fastcgi_temp_file_write_size 256k;
                        fastcgi_read_timeout 1200;
        }


        location ~ /\.(js|css)$ {
                expires 604800s;
        }

        if (!-e $request_filename){
                rewrite ^/(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ /$1.$3 last;
        }

        location ~* ^/fileadmin/(.*/)?_recycler_/ {
                deny all;
        }
        location ~* ^/fileadmin/templates/.*(\.txt|\.ts)$ {
                deny all;
        }
        location ~* ^/typo3conf/ext/[^/]+/Resources/Private/ {
                deny all;
        }
        location ~* ^/(typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) {
        }

    location ~* ^/qa/.* {
        allow all;
    }

        location / {

                        if ($query_string ~ ".+") {
                                return 405;
                        }
                        if ($http_cookie ~ 'nc_staticfilecache|be_typo_user|fe_typo_user' ) {
                                return 405;
                        } # pass POST requests to PHP
                        if ($request_method !~ ^(GET|HEAD)$ ) {
                                return 405;
                        }
                        if ($http_pragma = 'no-cache') {
                                return 405;
                        }
                        if ($http_cache_control = 'no-cache') {
                                return 405;
                        }
                        error_page 405 = @nocache;

                        try_files /typo3temp/tx_ncstaticfilecache/$host${request_uri}index.html @nocache;
        }

        location @nocache {
                        try_files $uri $uri/ /index.php$is_args$args;
        }
}

Now, I want to host a second application in the subfolder of the root of the TYPO3, which is packagist/packagist/web. This Packagist app requires a rewrite URL rules to work properly. Its normal vhost config would something like this:


server {
    listen 80;
    server_name yourserver.loc;
    root /var/www/packagist/web;
    index index.php index.html;     

    location / {                
        # try to serve file directly, fallback to rewrite
        try_files $uri @rewriteapp;                
        satisfy any;                        
    }                

    location @rewriteapp {                
        rewrite ^(.*)$ /app.php/$1 last;        
    }    

    location ~ ^/app\.php(/|$) {        
        fastcgi_pass 127.0.0.1:9001;              
        fastcgi_split_path_info ^(.+\.php)(/.*)$;        
        include fastcgi_params;               
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;        
        fastcgi_param DOCUMENT_ROOT $realpath_root;                
        internal;    
    }    

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.

    location ~ \.php$ {      
        return 404;    
    }    

    error_log /var/log/nginx/packagist_error.log;    
    access_log /var/log/nginx/packagist_access.log;
}

How can I merge those 2 configurations? I was trying to add the following to my TYPO3 vhost but the URL rewrite doesn't work.


location ^~ /packagist/packagist/web/.* {
                #root /var/www/typo3dev/web/packagist/packagist/web;
                alias /var/www/typo3dev/web/packagist/packagist/web;
                index index.php index.html;

                #try_files $uri $uri/ /packagist/packagist/web/app.php;
                try_files $uri @rewrite;

                location ~ \.php {
                        fastcgi_split_path_info ^(.+\.php)(/.*)$;
                        include /etc/nginx/fastcgi_params;
                        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_param PATH_INFO $fastcgi_path_info;
                        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                }
        }

    location @rewrite {
                rewrite ^(.*)$ /app.php/$1 last;
        }

At present if I want to use URL e.g. http://example.com/packagist/packagist/web/app.php/register I'm getting 404 page.

Please help :)

0

This might not solve your issue, but is a clear misconfiguration:

location ^~ /packagist/packagist/web/.* does not work in your solution. ^~ is a prefix match that causes nginx not to check regular expressions after the prefix match. So, in this case it matches all requests to URIs starting with literal /packagist/packagist/web/.*, and I doubt that you have any requests like that in your application.

You need to use location ^~ /packagist/packagist/web to match all requests starting with that prefix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.