How to List All Vhosts in Nginx

How can I list all vhosts in nginx

Update:
Thanks to @Putnik for pointing out an easier way (but I prefer only listing sites-enabled):

grep server_name /etc/nginx/sites-enabled/* -RiI

Old Post:

Try something like this:

find /etc/nginx/sites-enabled/ -type f -print0 | xargs -0 egrep '^(\s|\t)*server_name'

Hope that helps!

Apache and ultimate config for nginx to serve all virtual hosts in the right way

What you do now is sending all the network traffic to 127.0.0.1:8080 without allowing Nginx to serve the static files.

What you should try is the following:

server {
listen 80;
server_name sky2high.net www.sky2high.net;
location / {
proxy_pass http://127.0.0.1:8080;
include /etc/nginx/conf.d/proxy.conf;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|tgz|gz|pdf|rar|bz2|exe|ppt|txt|tar|mid|midi|wav|bmp|rtf) {
root /folder/to/static/files;
expires 90d;
}
location ~* ^.+\.(css|js)$ {
root /folder/to/static/files;
expires 30d;
}

And in proxy.conf you put the following:

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 8m;
client_body_buffer_size 256k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 32 256k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 256k;

This should work for you

How does nginx virtualhosts know the domain?

Nginx knows which domain to serve by looking at the Host HTTP header from the request. For example, if you have a server with several vhosts (assuming domain1.com and domain2.com), you can get root HTML page from your vhosts using the following curl commands:

curl -H 'Host: domain1.com' <server_ip>

and

curl -H 'Host: domain2.com' <server_ip>

Also, for every combination of IP and port on which nginx is listening, one of the vhosts will act as the default one. See How nginx processes a request official documentation page (or this answer) for more details on how the default vhost is selected.

How to setup mass dynamic virtual hosts in nginx?

You will need some scripting knowledge to put this together. I would use PHP, but if you are good in bash scripting use that. I would do it like this:

  1. First create some folder (/usr/local/etc/nginx/domain.com/).

  2. In main nginx.conf add command : include /usr/local/etc/nginx/domain.com/*.conf;

  3. Every file in this folder should be different vhost names subdomain.conf.

You do not need to restart nginx server for config to take action, you only need to reload it : /usr/local/etc/rc.d/nginx reload

OR you can make only one conf file, where all vhosts should be set. This is probably better so that nginx doesn't need to load up 50 files, but only one....

IF you have problems with scripting, then ask question about that...



Related Topics



Leave a reply



Submit