I am unable to get the subdomain from the URL in NGINX

jnbdz asked:

I am unable to get the subdomain from the URL in NGINX.

Here is my config:

server {
    listen  80;
    server_name     ~^(?<appname>).example.com$;

    rewrite ^ https://$appname.example.com$request_uri? permanent;
}

When I do:

http://bob.example.com/

I am sent to:

https://.example.com/

I don’t know what I am doing wrong.

I am using NGiNX 1.2.7.

I have another config for the:

http://example.com/

So I have one server block for the domain without the subdomain and the second with the subdomain… This is about the subdomain.

UPDATE

The redirect here (this is the reason I am trying to extract the subdomain):

server {
    listen 443 ssl;

    server_name     ~^(?<appname>).example.com$;

    ssl                     on;
    ssl_certificate         /etc/ssl/certs/example.com.crt;
    ssl_certificate_key     /etc/ssl/private/example.key;

    root    /var/www/example.com/apps/$appname/;

    include /var/nginx/general/php;
    include /var/nginx/general/upload;
    include /var/nginx/general/error_page_50x;
}

NEW UPDATE

New errors I am getting (this is without the P):

2013/06/30 00:49:02 [error] 7707#0: *64 directory index of "/var/www/example.com/apps//" is forbidden, client: 00.000.000.00, server: ~^(?<appname>).example.com$, request: "GET / HTTP/1.1     ", host: "ebooks.example.com"
2013/06/30 00:49:02 [error] 7707#0: *64 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 00.000.000.00, server: ~^(?<appname>).example.com     $, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ebooks.example.com"
2013/06/30 00:49:04 [error] 7707#0: *64 directory index of "/var/www/example.com/apps//" is forbidden, client: 00.000.000.00, server: ~^(?<appname>).example.com$, request: "GET / HTTP/1.1     ", host: "ebooks.example.com"
2013/06/30 00:49:04 [error] 7707#0: *64 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 00.000.000.00, server: ~^(?<appname>).example.com     $, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ebooks.example.com"

This is with P after the ?:

2013/06/30 00:55:57 [error] 17915#0: *74 directory index of "/var/www/example.com/apps//" is forbidden, client: 00.000.000.00, server: ~^(?P<appname>).example.com$, request: "GET / HTTP/1     .1", host: "drive.example.com"
2013/06/30 00:55:57 [error] 17915#0: *74 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 00.000.000.00, server: ~^(?P<appname>).example.c     om$, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "drive.example.com"

My answer:


All you’re doing is redirecting HTTP to HTTPS, so you don’t need this regex at all.

Instead, do something like this:

server {
    listen [::]:80;
    server_name *.example.com;

    return 301 https://$http_host$request_uri$is_args$args;
}

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.