0

I have a setup with my blog at example.com

server { listen 8082; ## listen for ipv4; this line is default and implied #listen [::]:80 default ipv6only=on; ## listen for ipv6 server_name example.com; 

And to redirect from www. to non-www i also have this block:

server { listen 8082; server_name www.example.com; return 301 http://example.com$request_uri; } 

This also works, but then i wanted to add a subdomain: "api.example.com". First i tried adding another file in sites-available and symlinking to sites-enabled. But that didn't work, the second file did not trigger at all.

Next i added the subdomain as a serverblock in the first file. That worked. But now every subdomain lead to api.example.com.

First i don't understand how "test.example.com" can lead to this serverblock:

server { listen 8082; ## listen for ipv4; this line is default and implied #listen [::]:80 default ipv6only=on; ## listen for ipv6 server_name api.example.com; } 

since server_name is api.example.com, and test.example.com is another subdomain. And how can i let every subdomain not specified lead to mainpage or custom error page?

3
  • Before i added the last block, the redirections block kicked in no matter what subdomain i typed. Commented Apr 1, 2014 at 11:57
  • So you have two files (links) in /sites-enabled/ folder? Commented Apr 1, 2014 at 12:10
  • No, now all 3 server blocks are in one file, the only one active. Commented Apr 1, 2014 at 12:18

1 Answer 1

1

First of all, to separate your 3 domains/subdomains server blocks into three different files may be a better practice for organization. As I understood, your nginx is configured to read .conf files from sites-enabled. The possibility of creating a link from sites-enabled to sites-available just let you easily disable sub domains/subdomains when you want without having to remove your configuration file (you just remove the symbolic link). This is good. But something may be wrong with your configuration. You can try, from this sample nginx.conf (source):

# Generic startup file. user {user} {group}; #ususally equal to number of CPU's you have. run command "grep processor /proc/cpuinfo | wc -l" to find it worker_processes 2; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; # Keeps the logs free of messages about not being able to bind(). #daemon off; events { worker_connections 1024; } http { # rewrite_log on; include mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; sendfile on; # tcp_nopush on; keepalive_timeout 3; # tcp_nodelay on; # gzip on; #php max upload limit cannot be larger than this client_max_body_size 13m; index index.php index.html index.htm; # Upstream to abstract backend connection(s) for PHP. upstream php { #this should match value of "listen" directive in php-fpm pool server unix:/tmp/php-fpm.sock; # server 127.0.0.1:9000; } include sites-enabled/*; } 

If you don't want/need to change all your nginx.conf file, just take a look at the last line (an include).

Then, just create your each server (vhost) config file in sites-enabled.

Regarding your issue to configure subdomains, please verify yourself that you are creating the respective DNS entries for the subdomains. This is something obvious, but please double check it as your nginx configuration seems to be correct. Than, create the server blocks using different files:

server { listen 8082; ## listen for ipv4; this line is default and implied #listen [::]:80 default ipv6only=on; ## listen for ipv6 server_name api.example.com; #the rest of your server config goes here. } 
Sign up to request clarification or add additional context in comments.

7 Comments

I'm not sure about that DNS-part, but my domain is pointed at my server, so that part is working. Is the order of the nginx server blocks important?
If you have your server IP as the response when running "ping subdomain.domain.com" from your Terminal than it's all right. The order does not matter. But when you're using external conf files there will be no order, just some "server vhosts"external files. Please use the suggestions from my answer. I've being using nginx for almost 3 years and I always configure subdomains as I suggested – so I can confirm this will do exactly what you want. ;)
I followed your example, and now it works with multiple files, the only thing that's not working is that if i go to "test.example.com" or anything else nginx shows the file for api.example.com. Can i create a *.example.com? And will it take anything but the specified subdomains?
Let me see if I understood: when you type test.example.com, the configuration file which is used is the one for api.example.com? Verify that all your server files have the "server_name" defined. If you have example.com, api.example.com and test.example.com, there will be three files. And for each file define a server block with server_name defined as the domain address (this is the address which nginx will listen to).
I have 3 server blocks now, api.example.com (in one file), and example.com + the redirect block from www.example.com to example.com in another file. The redirect block is only 4 lines, so i thought it made sense to let it stand next to the main block. The problem is that for every other subdomain [randomtext].example.com the api.example.com index.php is served.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.