1

I did some reading and I can not find an answer to my question as it has one key component that is different from majority of cases. It starts the same as every other story: I need to migrate .htaccess in to nginx config which would be quite simple if not for this: nginx server is set up to use dynamic hosts:

server {
    listen 80;

    server_name ~^(www\.)?(?<sname>.+?).server.company.com$;
    root /var/www/$sname/current/public;
    index index.html index.htm index.php;

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

    location ~* \.(gif|png|bmp|ico|flv|swf|exe|html|htm|txt|css|js) {
        add_header        Cache-Control public;
        add_header        Cache-Control must-revalidate;
        expires           7d;
    }

    location ~ \.php$ {

        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        include fastcgi_params;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
    }

    location ~ /\.ht {
        deny all;
    }
}

This is done to run multiple projects on a single subdomain based on their directory. Problem is that one of those projects (lets call it theproject.domain.company.com) is a very old behemoth that uses .htaccess for a ton of redirects. I can create the location blocks for those redirects, but I have no clue how to apply them to only that project (I am not very experienced with nginx).

I am willing to accept any possible solution and what i have theorised about is:

1) directory specific nginx config - kind of like htaccess but not sure if nginx is even capable of loading config on the fly

2) using if block for specific server name but not sure about the syntax as i could not find any examples of if being used for server name

3) separate vhost for that subdomain, this would be a viable, allthough not a very gracefull solution for my issue, problem is I do not know how to set up priorities as that subdomain will match the same pattern of the dynamic vhost

Any help, or advice or links are very appreciated

2

Option (3) reduces the risk breaking every subdomain just to fix one rogue subdomain. A server block with a server_name that is an exact match will always take priority over a regular expression server_name. See this document for details.

If you want to minimise duplicated configuration, offload the common statements to a separate file and pull them in using an include statement.

For example:

server {
    listen 80;
    server_name www.theproject.server.company.com theproject.server.company.com;

    root /var/www/theproject/current/public;

    #
    # ... statements to fix "theproject"
    #

    include /path/to/common/config;
}

server {
    listen 80;
    server_name ~^(www\.)?(?<sname>.+?).server.company.com$;

    root /var/www/$sname/current/public;
    include /path/to/common/config;
}

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.