248

I installed Nginx on Centos 6 and I am trying to set up virtual hosts. The problem I am having is that I can't seem to find the /etc/nginx/sites-available directory.

Is there something I need to do in order to create it? I know Nginx is up and running because I can browse to it.

Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
Fr0ntSight
  • 2,696
  • 3
  • 17
  • 14
  • If you want to split one large config file into smaller ones, you can use my script to split it: http://stackoverflow.com/a/38635284/1069083 – rubo77 Jul 28 '16 at 12:04

3 Answers3

468

Well, I think nginx by itself doesn't have that in its setup, because the Ubuntu-maintained package does it as a convention to imitate Debian's apache setup. You could create it yourself if you wanted to emulate the same setup.

Create /etc/nginx/sites-available and /etc/nginx/sites-enabled and then edit the http block inside /etc/nginx/nginx.conf and add this line

include /etc/nginx/sites-enabled/*;

Of course, all the files will be inside sites-available, and you'd create a symlink for them inside sites-enabled for those you want enabled.

user2066657
  • 444
  • 1
  • 4
  • 23
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
  • 37
    RedHat relatives (i.e. Fedora and Centos) don't have this feature out of the box like Debian/Ubuntu. It's easy to copy, as @Mohammad said, however not having nice defaults is confusing for newcomers. – MGP Sep 16 '13 at 19:17
  • 59
    God, I can't say how many searches I did to figure this out. I am using Amazon Linux AMI release 2014.03. These directories are not created automatically if you install nginx from the rpm files(whether nginx 1.6.2 or nginx 1.7.5) but many nginx configuration examples refer these...making you wonder whether your nginx got installed correctly or not. – so_mv Sep 23 '14 at 00:30
  • I tried this on my Amazon Linux AMI, but it is not working. Do I need to do anything else? – DBZHOU Sep 07 '16 at 18:59
  • @JiayiZhou not at all, just reload nginx, if it's not working then explain what you did so I could help you. – Mohammad AbuShady Sep 07 '16 at 19:00
  • I followed (https://www.sitepoint.com/deploy-your-rails-app-to-aws/) to deploy my ruby on rails app to the server. I did it sucessfully if my server uses ubuntu. But I need to deploy the app to a server using Amazon Linux AMI. I thought it should be the same. So I did exactly the same steps untill the config of nginx. In Amazon Linux AMI, there is no folder sites-enabled and sites-available. So I created these two folder. Put the config file in sites-available, create the symlink in sites-enabled, add include /etc/nginx/sites-enabled/*; in /etc/nginx/nginx.conf. But not working... – DBZHOU Sep 07 '16 at 19:09
  • shouldn't it be ```*.conf``` ? Also is this how you synlink a new conf file? ```sudo ln -s sites-available/test.conf sites-enabled/test.conf``` – zeros-and-ones Sep 24 '16 at 22:07
  • @zeros-and-ones I guess you could say that, even though I don't consider this a wrong and right kind of thing, just a preference. – Mohammad AbuShady Sep 24 '16 at 22:11
  • 1
    Cool thanks, how about the symlink? Is my syntax correct? It doesn't seem to be getting found by the include statement. – zeros-and-ones Sep 24 '16 at 22:20
  • when i run ```ls -l``` I get this: test.conf -> sites-available/test.conf so it looks correct, but the user shows root not the user that own the original test.conf – zeros-and-ones Sep 24 '16 at 22:21
  • @zeros-and-ones just to make sure before we investigate, did you reload nginx? – Mohammad AbuShady Sep 24 '16 at 22:24
  • yes restarted it after each change. i'll try again. – zeros-and-ones Sep 24 '16 at 22:26
  • 9
    I fixed it, started fresh and created sym link like this: ```sudo ln -s /etc/nginx/sites-available/test.conf /etc/nginx/sites-enabled/test.conf``` not sure why the full path made a difference :/ – zeros-and-ones Sep 24 '16 at 22:36
  • What we are missing now are scripts to enable/disable websites with autocompletion ^^ (Like `a2ensite` and `a2dissite` for Apache) – Cyril Duchon-Doris May 29 '17 at 13:42
  • Hey I believe I followed all the steps correctly, but it doesn't seem to reroute from port 80 to 3000. here's an SO question I posted, detailing exactly what I did: https://stackoverflow.com/questions/44978535/cant-get-nginx-to-reroute-from-test-page-on-port-80-to-3000-using-linux-ami – VDog Jul 07 '17 at 19:44
  • does the server_name variable matter in the test.conf? inside sites-available/ ? – VDog Jul 07 '17 at 19:48
  • 1
    Thank you so much. Built nginx from source and didn't realize ubuntu did this. – Sean Norwood Dec 07 '17 at 20:12
  • This can cause `default_server` problems if you are switching between different nginx images. Basically if you have a `default_server` setup in your nginx config remember to remove the default config in `/etc/nginx/sites-available` if installing from a debian based image. – Edward May 02 '18 at 16:31
  • To avoid editing the default configuration file, I opt for creating a new file at `/etc/nginx/conf.d/sites-enabled-includes.conf` and put your `include` directive in there. (The *conf.d* directory exists by default in CentOS 7, nginx/1.12.2) – sshow Nov 01 '18 at 15:01
  • 1
    I'm used to having the folders *sites-available* and *sites-enabled* present but since moving from Mint to Manjaro I had to do this manually. Simply create the folders, cut the existing default server block from */etc/nginx/nginx.conf* and paste it into the default file **/etc/nginx/sites-available/default**. Customize this as needed. Once done, make a sym link in the *sites-enabled* folder then test if everything is ok: `sudo nginx -t`. If there were no problems then restart nginx: `sudo systemctl restart nginx`. – enchance Feb 06 '20 at 13:59
  • @MohammadAbuShady , I'm running `nginx` via docker container. How do I correct it in this case? Thank you – GitHunter0 Feb 14 '21 at 00:53
  • @GitHunter0 Probably you can mount the config as a volume, what do mount when you run the nginx container ? – Mohammad AbuShady Feb 15 '21 at 12:38
  • @MohammadAbuShady, in docker-compose I just link the nginx.conf file but I will have to inspect the Dockerfile that generated nginx container for other information – GitHunter0 Feb 16 '21 at 14:20
84

If you'd prefer a more direct approach, one that does NOT mess with symlinking between /etc/nginx/sites-available and /etc/nginx/sites-enabled, do the following:

  1. Locate your nginx.conf file. Likely at /etc/nginx/nginx.conf
  2. Find the http block.
  3. Somewhere in the http block, write include /etc/nginx/conf.d/*.conf; This tells nginx to pull in any files in the conf.d directory that end in .conf. (I know: it's weird that a directory can have a . in it.)
  4. Create the conf.d directory if it doesn't already exist (per the path in step 3). Be sure to give it the right permissions/ownership. Likely root or www-data.
  5. Move or copy your separate config files (just like you have in /etc/nginx/sites-available) into the directory conf.d.
  6. Reload or restart nginx.
  7. Eat an ice cream cone.

Any .conf files that you put into the conf.d directory from here on out will become active as long as you reload/restart nginx after.

Note: You can use the conf.d and sites-enabled + sites-available method concurrently if you wish. I like to test on my dev box using conf.d. Feels faster than symlinking and unsymlinking.

030
  • 10,842
  • 12
  • 78
  • 123
elbowlobstercowstand
  • 3,812
  • 1
  • 25
  • 22
  • 19
    The point of the symlinking "mess" is that you can preserve config files and still disable virtual hosts very easily. If you use your approach, you have to move the files out of conf.d when you want to disable a site which I don't think is any less messy than conveniently deleting a symlink. – bviktor Sep 29 '14 at 21:43
  • @bviktor I hear ya. I actually use both ways. I just found it more tedious [when testing configs] to bounce back and forth between the sites-available and sites-enabled dirs. In my production env I'm planning on using symlinking. – elbowlobstercowstand Sep 30 '14 at 06:08
  • 3
    Yeah, I agree, typing ln -s `../sites-available/...` is rather tedious when quickly testing stuff, but in the long run it's handy and easier to manage :) – bviktor Oct 01 '14 at 09:33
  • @bviktor So thankful for the ability to be able to hit tab in terminal when writing a path name and watch auto-complete do it's thing. Didn't know about that for a while until I watched a video with someone using it. I thought they typed at 300 wpm! Then I caught on. :) – elbowlobstercowstand Oct 03 '14 at 00:44
  • 14
    @bvktor Only sites with .conf extension are getting included. If you don't want a site to be enabled, just add .disabled extension to the site configuratoin file. That's the standard convention in CentOS / Apache and it will apply to Nginx as well. – SenG Nov 10 '15 at 01:30
  • 2
    a little surprised no one has suggested to `include /etc/ngins/conf.d/*.enabled` or something similar, disabling a virtual host would require just to rename the _.enabled_ away from the name – Purefan May 22 '20 at 23:34
-5

I tried sudo apt install nginx-full. You will get all the required packages.