Question

Can we host multiple websites in nginx at localhost?

Posted August 1, 2013 53.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?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
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
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?
@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
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
Previous 1 2 Next