Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Nginx virtualhosts work? (currently gives 403 Forbidden error)

I want to run nginx on my Ubuntu 10.04 32bit Linode VPS.

sudo chown -R www-data:www-data /var/www
sudo chmod -R 775 /var/www

sudo add-apt-repository ppa:nginx/development
sudo apt-get update
sudo apt-get install nginx

To make a nginx virtualhost:

mkdir -p /var/www/example.com/{public,logs}
sudo nano /etc/nginx/sites-available/example.com

and wrote following

server {
     listen   80;
     server_name  www.example.com;
     rewrite ^/(.*) http://example.com/$1 permanent;
}

server {
     listen   80;
     server_name example.com;

     access_log /var/www/example.com/logs/access.log;
     error_log /var/www/example.com/logs/error.log;

     location / {
          root   /var/www/example.com/public/;
          index  index.html;
     }
}

Then I enabled the virtualhost example.com

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
sudo /etc/init.d/nginx restart

I put a index.html to /var/www/example.com/public and enter www.example.com URL from my browser. Then I got following

403 Forbidden
nginx/0.8.53

tail /var/log/nginx/error.log gives following error

*38 directory index of "/var/www/" is forbidden, client: 88.224.1.128, server: localhost, request: "GET / HTTP/1.1", host: www.example.com

I redo

sudo chown -R www-data:www-data /var/www
sudo chmod -R 775 /var/www

but it gives same error.

and cat /etc/nginx/nginx.cnf

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}
http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    # As per: http://wiki.nginx.org/NginxHttpGzipModule#gzip_disable starting
    # with 0.7.63
    gzip_disable     "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

If I put the index.html to /var/www/ then I can see html file but any attempt to put it under /var/www/example.com/public fails. I looked at file and folder permissions they are ok all belongs to www-data and readable (775)

What can I do to make nginx work? Thanks

like image 889
Gok Demir Avatar asked Nov 23 '10 08:11

Gok Demir


3 Answers

The 403 is because have off the autoindex.

autoindex off;

Need put it on, and you can see the dirs

autoindex on;

If is off you can see the files but not list the directories.

like image 69
Skamasle Avatar answered Oct 11 '22 19:10

Skamasle


I had the same symptoms on Ubuntu 10.04, Nginx & Wordpress and the answer was a desperately dumb one.

In case someone else is in the same situation....

In my nginx config for the site

location / {
        index  index.html index.htm;
    }

didn't have index.php

adding it to the list cured the problem

like image 42
steve creedon Avatar answered Oct 11 '22 19:10

steve creedon


I suppose, that

rewrite ^/(.*) http://example.com/$1 permanent;
doesn't works properly. Host was remained www.example.com No redirect was happened, since you get following error line into your log:
*38 directory index of "/var/www/" is forbidden, client: 88.224.1.128, server: localhost, request: "GET / HTTP/1.1", host: www.example.com

Try to play with rewrite line.

Another version: Are you sure, that those lines is works correctly?

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
like image 21
CyberDem0n Avatar answered Oct 11 '22 19:10

CyberDem0n