Wednesday, May 15, 2019

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




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



Here is my config:



server {
listen 80;
server_name ~^(?)\.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 ~^(?)\.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: ~^(?)\.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: ~^(?)\.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: ~^(?)\.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: ~^(?)\.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)\.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)\.example\.c om$, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "drive.example.com"


Answer



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 ~^(?.+)\.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;
}

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...