Host multiple website using nginx on the same IP
In the server section of the config you can specfify the server_name and the related root, so e.g. in /mnt/web/nginx/conf.d/ you create 2 .conf files with
e.g.
server {
server_name domain1.co.uk;
root /mnt/web/sites/dom1site;
access_log /var/log/nginx/domain1.co.uk.access_log;
error_log /var/log/nginx/domain1.co.uk.error_log;
include /etc/nginx/global/restrictions.conf;
include /etc/nginx/global/wordpress.conf;
}
and
server {
server_name Domain2.it;
root /mnt/web/sites/dom2site;
access_log /var/log/nginx/domain2.it.access_log;
error_log /var/log/nginx/domain2.it.error_log;
include /etc/nginx/global/restrictions.conf;
include /etc/nginx/global/wikipedia.conf;
}
here is a handy link to configuration help: http://wiki.nginx.org/Configuration
So... in your main nginx.conf you specify the http {} where you can reference a directory where you have all the configs for the individual site configs (probably the handiest).
example (!) of nginx.conf:
user rasp-user rasp-user;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
tcp_nopush off;
tcp_nodelay on;
sendfile on;
types_hash_max_size 2048;
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 15;
send_timeout 10;
client_body_buffer_size 8K;
client_header_buffer_size 1k;
client_max_body_size 20m;
large_client_header_buffers 2 1k;
gzip on;
gzip_comp_level 2;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/xml;
gzip_disable "MSIE [1-6]\.";
index index.php index.html index.htm;
upstream php {
#server unix:/tmp/php-fpm.sock;
server 127.0.0.1:9000;
}
include /etc/nginx/conf.d/*.conf;
include /mnt/web/nginx/conf.d/*.conf;
}
Related videos on Youtube
Mitro
Updated on September 18, 2022Comments
-
Mitro over 1 year
I've set up a Raspberry Pi to act as web server, so I've installed nginx with php5 and I bought two domains:
- domain1.co.uk
- Domain2.it
I have a static IP which is linked to these two domains, so if I type one or the other I get the same example web page that I wrote.
How could I do to set a directory for a domain and another for the other domain?
-
MonkeyZeus over 9 yearsI like that you're using NGINX :) You are looking to implement something called virtual hosting. This has been available with Apache since 1995 I believe so it would be rather silly if NGINX did not support it lol. I didn't read the whole thing but here is some history reading: httpd.apache.org/ABOUT_APACHE.html
-
Mitro over 9 yearsYes, that's helpful. Thank you. That wasn't listed in related question, maybe the title was too different from mine.
-
MonkeyZeus over 9 yearsYou're welcome and good luck!
-
Aboba over 9 yearsI just throw the nodes into the main file, but same result.
-
Mitro over 9 yearsyou don't use default in sites-available?