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:
First create some folder (/usr/local/etc/nginx/domain.com/).
In main nginx.conf add command : include /usr/local/etc/nginx/domain.com/*.conf;
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
Black Color Showing on Cmy Channels When Converted to Cmyk Using Ghostscript
Every Command Is Returning 'Bash: <Command>: Command Not Found...'
How to Delete All Files Older Than 3 Days When "Argument List Too Long"
How to Make Find and Printf Works in Bash Script
How to Append to a File Using X86-64 Linux System Calls
About Process Control Block in Os
Escape Single Quotes in Shell Script
Trailing Arguments with Find -Exec {} +
Adding a Header into Multiple .Txt Files
I Want to Remove Multiple Line of Text on Linux
Grabbing Specific Sections of Text from a String
Why Is Cpu-Cycles Much Less Than CPU Current Frequency
Lsb_Release: Command Not Found in Latest Ubuntu Docker Container
How to Show the Wget Progress Bar Only
How to Write Non-Ascii Characters Using Echo