0

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"

3 Answers 3

2

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;
}
4
  • This was just for testing. The redirect goes to a another server block where the subdomain is used in the root: root /var/www/sitenetapps.com/apps/$appname/;
    – jnbdz
    Jun 28, 2013 at 3:37
  • You should have said that originally. Jun 28, 2013 at 3:38
  • But thanks I am going to use your recommendation for the redirect.
    – jnbdz
    Jun 28, 2013 at 3:38
  • So what do I do?
    – jnbdz
    Jun 28, 2013 at 15:29
0

If you're running CentOS 5 (or another older operating system), it might be due to an older PCRE library. The Python-compatible syntax might work for you:

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

The change is the P after the ?. This is supported in PCRE 4.0 or higher, which the method without P requires PCRE 7.0 or higher.

This is explained in the Server names page of the Nginx manual, under Regular expression names.

1
  • I tried your way but NGINX is still for some reason unable to get the value from the subdomain. I added the errors to my question.
    – jnbdz
    Jun 30, 2013 at 0:59
0

I just needed to add .+ after ?< subdomain >:

server {
    listen  80;
    server_name     *.example.com;

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

server {
    listen 443 default_server ssl;

    server_name     ~^(?<subdomain>.+)\.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/$subdomain/;

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .