A
A
Andrey2017-02-18 23:43:12
Nginx
Andrey, 2017-02-18 23:43:12

Wildcard subdomain in Nginx returns No input file specified. What's wrong?

There is a domain on which, in order to show the results to the client, I would like to make dynamic subdomains that would lead to folders in the subdomains directory at the root. I write this config:

server {
        listen 80;
        server_name ~^(.*)\.example\.com$;

        root /var/www/html/example.com/public_html/subdomains/$1;

        index   index.php index.html index.htm;

        # Security
        include common/security;

        # Gzip
        include common/gzip;

        location ~ \.php$ {
                root /var/www/html/example.com/public_html/subdomains/$1;
                include common/php-fpm;
        }
}

Returns No input file specified when accessing a subdomain . . The owner of www-data:www-data. What can be wrong?
UPD I'll post the full config, maybe the problem is in some part.
include common/upstream;

server
{
  listen 80;
  root	/var/www/html/example.com/public_html;
  index	index.php index.html index.htm;
  server_name	example.com;
  
  error_log "/var/www/html/example.com/logs/error.log";
  access_log "/var/www/html/example.com/logs/access.log";
  
  # увеличение максимального объема файла для загрузки до 200МБ
  client_max_body_size		5m;
  
  # Buffers
  fastcgi_buffers 64 4K;
  
  # Security
  include common/security;
  
  # Gzip
  include common/gzip;
  
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ \.php$ {
    include common/php-fpm;
  }

  # Кеширование
  include common/cache;

  # Запрещенные файлы
  include common/deny;
}

# 301 redirect from www
server {
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

# subdomain for static content
server {
  listen 80;
  server_name st.example.com;
  root /var/www/html/example.com/public_html;

  # Запрещенные файлы
  include common/deny;
  
  # Кеширование
  include common/cache;

  fastcgi_hide_header Set-Cookie;
}

# dynamic subdomains
server {
        listen 80;
        server_name ~^(.*)\.example\.com$;

        root /var/www/html/example.com/public_html/subdomains/$1;

        index   index.php index.html index.htm;

        # Security
        include common/security;

        # Gzip
        include common/gzip;

        location ~ \.php$ {
                root /var/www/html/example.com/public_html/subdomains/$1;
                include common/php-fpm;
        }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
Wexter, 2017-02-19
@Wexter

location / {
try_files $uri $uri/ =404;
}

A
Andrey Schultz, 2017-02-19
@noroots

Alas, I still can't decide.

D
Dmitry MiksIr, 2017-02-19
@miksir

You are using numbered selections, but they reset after each regexp. Those. `location ~ \.php$` no longer has the $1 you expect.
As a workaround, assign $1 to some variable `set $servername $1`, and the best is to use named selections
`server_name ~^(?<servername>.+)\.example\.com$;`
and then use $servername anywhere in the server config.
It is worth reading nginx.org/ru/docs/http/ngx_http_core_module.html#s... and nginx.org/ru/docs/http/server_names.html
It also describes the order in which server names are handled.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question