How to access to folder without domain using nginx and CentOS?

October 18, 2014 5.4k views

Hi!

I have a big doubt about how to load the content of a folder in the browser without use a domain using nginx in CentOS… I mean, like apache works, for example:

If I have in /var/www/html/page1 and /var/www/html/page2 I can open those folders in the url with this

myip/page1
my
ip/page2

So, I want to do the same in my droplet… I have many folders in my www folder and I want to access to these folders like the above.

I want to do this because, I want to provide access to the demos of my apps of my portafolio to the users.

Thanks in advance

3 comments
1 Answer

Set an Nginx vhost to the root where your demos are stored, like this:

server {
        listen 80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www/html/;
        index index.html index.htm index.php;

        # Make site accessible from http://localhost/
        server_name .domainname-goes-here.com;


        error_page  404              /404.html;
        location = /404.html {
        root   /usr/share/nginx/html;
        }

        # redirect server error pages to the static page /50x.html
        #
                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                root   /usr/share/nginx/html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                #try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

}

Then just go to the domain/subdomain you set for it and add the folder name ir example.com/page2/

make sure to have a index file (index.php, html, etc..).

if you want it to be accesible via IP you need to make it your default website/virtual host by changing this line:

# Make site accessible from http://localhost/
server_name .domainname-goes-here.com;

to

# Make site accessible from http://localhost/
server_name _;

but it looks professional to use a domain/subdomain and is easier to remember. make sure you don’t have another virtual host as default one!

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!