Can we host multiple websites in nginx at localhost?

August 1, 2013 24.9k views
I have a general question about nginx Let say you don't have a domain first, but you already fired up your droplets. You already success setup Nginx in your droplet. But your plan is to host 2 website in the droplet So the first website is done, assuming the default nginx page is the first website. Now, how can i host the second website? Remember for now i still dont have the domains. What to put in the nginx configuration (server_name) for the second website? For the first website, i already use "server_name localhost;" What sould i put for the second website?
11 Answers
I prefer creating separate virtualhosts for each app and setting server_name to app_name.dev;

Edit your /etc/hosts file and add the following line:
127.0.0.1 app1.dev app2.dev etc.dev;

You should be able to access your apps by browsing to http://app_name.dev
@Dzulhelmi: Create a second virtualhost for the second app/website, and set its server_name to e.g. app1.dev:

https://www.digitalocean.com/community/articles/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3
by Etel Sverdlov
This tutorial shows you how to set up a new directory, how to grant the correct permissions, how to add content to a new page, how to turn on virtual hosting, and how to, if needed, set up local hosts. Virtual Hosts are used to run more than one website or domain off of a single server and IP address. When used with nginx, virtual hosts are called Server Blocks. This tutorial covers setting up server blocks on an Ubuntu server.
by Etel Sverdlov
This tutorial shows you how to set up a new directory, how to grant the correct permissions, how to add content to a new page, how to turn on virtual hosting, and how to, if needed, set up local hosts. Virtual Hosts are used to run more than one website or domain off of a single server and IP address. When used with nginx, virtual hosts are called Server Blocks. This tutorial covers setting up server blocks on an Ubuntu server.
i did, but that tutorial assuming you already have a domain, or point to ip address. Its not answering my question here
Just b/c you don't have a domain registered doesn't mean you can not create additional server blocks. Developers use fictitious domain names all the time for development environments, e.g. local.wordpress.dev

Perhaps I did not understand the question, but were you not asking what name you should assign to the server_name variable on the second server block?
Yes, because the first server block i already use server_name localhost;

For the second, i try use server_name localhost/foldername; but nginx cannot accept nonalphabet character in server_name
Yes, because the first server block i already use server_name localhost;

For the second, i try use server_name localhost/foldername; but nginx cannot accept nonalphabet character in server_name
Yes, because the first server block i already use server_name localhost;

For the second, i try use server_name localhost/foldername; but nginx cannot accept nonalphabet character in server_name
If your goal is to "use server_name localhost/foldername", you simply need some rewrite directives. See Nginx's HttpRewriteModule.
Kamal,

I dont get it. Let say currently my localhost already have one website. Which is can be opened by http://localhost

And right now i want to add one more website since nginx can serve more than one website, ok let say i edit my hosts file like what you wrote there.

Now in my nginx configuration for this second website, what should i put for the server_name?
Since the localhost is already pointing the default first website?

Just add some bogus domains to your 'hosts' file in the droplet. set up nginx using those. Then add them to your client machine 'hosts' file also. Now your bogus domains are available to you only and work in nginx.

Have another answer? Share your knowledge.