FreeBSD: Nginx Virtual Hosting Configuration
Q. How do I configure Nginx Web server for virtual hosting (host multiple websites under same IP address – name based virtual hosting)?
A. Nginx allows you to host more than one domain name on the same computer and on the same IP address. There are two basic methods of accomplishing virtual hosting: name-based, and IP address or ip-based. This tutorial covers name-based virtual hosting i.e.
you can hosts multiple websites (host names) for the same webserver IP address.
You need to create directory structure as follows to host more than two websites under same IP address:
a] /websites : Host each domain under this directory. You need to create dirs as follows:
b] /usr/local/etc/nginx/vhosts : Host each domains configuration under this directory.
Type the following commands:
Create / update default cache all ngnix config file
Open /usr/local/etc/nginx/nginx.conf file, enter:
Update it as follows:
user www www ; worker_processes 1 ; # main server error log error_log /var/log/nginx/error.log ; pid /var/run/nginx.pid ; events { worker_connections 1024 ; } # main server config http { include 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» ‘ ; sendfile on ; #tcp_nopush on ; #keepalive_timeout ; keepalive_timeout 65 ; gzip on ; # default server for ip 202.54 .1 .2 server { listen 202.54 .1 .2 : 80 default ; server_name _ ; access_log /var/log/nginx/access.log main ; server_name_in_redirect off ; location / { index index.html ; root /usr/local/www/nginx ; } } # virtual hosting include /usr/local/etc/nginx/vhosts/* ; }
Source: