Nginx : Virtual hosting using Location directives
This setup demonstrates a virtual container-based environment in Nginx where multiple locations are configured under a single server block, rather than a load-balancing mechanism. Each location serves a different application based on its respective directory structure.
Nginx Configuration
Server Configuration
server {
listen 80;
server_tokens off;
server_name _;
client_max_body_size 35M;
charset utf-8;
large_client_header_buffers 4 16k;
root /usr/share/nginx/server1;
index index.html index.php index.cgi;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /server2 {
root /usr/share/nginx/;
index index.html index.php index.cgi;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
}
location /server3 {
root /usr/share/nginx/;
index index.html index.php index.cgi;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
}
}Directory Structure
Each application is stored under a separate directory within /usr/share/nginx/.
Application Directories and Content
[root@server1 conf.d]# cat /usr/share/nginx/server1/index.html
Application 1
[root@server1 conf.d]# cat /usr/share/nginx/server2/index.html
Application 2
[root@server1 conf.d]# cat /usr/share/nginx/server3/index.html
Application 3Explanation
Main Root (
/usr/share/nginx/server1/): This serves as the default root for the server.Location ****
/server2:Serves content from
/usr/share/nginx/server2/Allows access through
http://server_ip/server2
Location ****
/server3:Serves content from
/usr/share/nginx/server3/Allows access through
http://server_ip/server3
Testing the Setup
Run the following commands to verify the setup:
curl http://localhost/ # Should return "Application 1"
curl http://localhost/server2 # Should return "Application 2"
curl http://localhost/server3 # Should return "Application 3"Conclusion
This setup allows serving multiple applications on different paths without using multiple virtual hosts or load balancing. It is useful for lightweight multi-container setups or development environments.
=======================================================================
Thank you, check out more blogs for such information..!
For any queries,feel free to reach out me on shubhammore07007@gmail.com

Comments
Post a Comment