Nginx is used as a web server. And you can host multiple websites on one Nginx web server use same or different port number. To achieve this is very easy, what you need to do is to configure multiple websites in Nginx configuration file, then deploy each website content to each related website document root folder.
1. Host Multiple WebSites On One Nginx Server Steps.
- First you should find the Nginx server configuration file, if you do not know how to find it, you can read article How To Find Where Nginx Is Installed And Locate The Nginx.conf File Which Nginx Is Actually Using.
- The Nginx server configuration file is generally named with nginx.conf.
- Edit it and add one server block in it to configure one website as below, you can add multiple server blocks in the nginx.conf file to add multiple websites.
server { # Website server listen port number, different website can listen same port number or different port number, this is decided by your requirements. But you should make sure the port number is not blocked by your firewall configuration. listen 80; # If your website enabled https then add below listening port. listen 443 ssl http2; # The domain name list that this website belongs to, domain names are separated by whitespace. When client request one domain in the list, this website will handle the request and send response. server_name test-example.com www.test-example.com; # The index page file name and file extension. If you remove one file ( for example default.htm ) from below list, then when you request this file, it will show you forbidden error page. index index.php index.html index.htm default.php default.htm default.html; # the website root folder path. you should upload your website content file ( html or php files ) in this folder. root /var/....../test-example.com; # if request server port is not 443 then redirect the request to 443 port. #HTTP_TO_HTTPS_START if ($server_port !~ 443){ rewrite ^(/.*)$ https://$host$1 permanent; } #HTTP_TO_HTTPS_END # SSL certificate file saved path. ssl_certificate /var/.../abc.pem; ssl_certificate_key /var/.../abc.privkey.pem; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers EECDH+CHACHA2......ES:RSA+3DES:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; add_header Strict-Transport-Security "max-age=31536000"; error_page 497 https://$host$request_uri; #SSL-END #ERROR-PAGE-START Error page configuration, you can comment, delete or modify #error_page 404 /404.html; #error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP reference configuration, you can comment or modify include enable-php-90.conf; #PHP-INFO-END #REWRITE-START URL rewriting rule reference, after modification, the pseudo-static rule set by the panel will be invalid include /var/....../test-example.com.conf; #REWRITE-END #Files or directories forbidden to access location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } #One-click application for SSL certificate verification directory related settings location ~ \.test-example{ allow all; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; error_log /dev/null; access_log off; } location ~ .*\.(js|css)?$ { expires 12h; error_log /dev/null; access_log off; } # Website access and error log file saved location. access_log /var/....../test-example.com.log; error_log /var/....../test-example.com.error.log; }
- If you find above one server block is too long, you can save it in a separate configuration file such as domain_name.conf ( test-example.conf ), then include the file in Nginx server nginx.conf file like below code.
include /var/....../domain_name.conf;
- For multiple websites you can create multiple domain_name.conf file to configure each website, then include them all in the Nginx server nginx.conf file.
- After you configure multiple websites, do not forget to restart the Nginx web server, I had told you how to do it in article How To Find Where Nginx Is Installed And Locate The Nginx.conf File Which Nginx Is Actually Using, please refer it.
References
- How To Verify Website Panel (ISPConfig, BT, WHM Etc ) Can Serve Multiple Website Use Same IP On Same Port Number
- How To Fix Nginx Redirect To Wrong Website Error When There Are Multiple WebSites
- How To Enable HTTPS For Nginx Website That Use CDN (CloudFlare)