Nginx : directory listing and serve static files

July 27th, 2015 § 0 comments § permalink

Recently I need to enable the directory listing in one nginx route and enable file download for team members.

Enable Directory listing

Enabling directory listing in nginx is pretty easy.

location ~ ^/files(|\/)$  {
  alias   /var/www/team_files/;
  autoindex on;

Here autoindex on; is the one enabling directory listing. alias points to the source file directory.

Serve Static files

This was relatively difficult part for me. I played a lot for this to work.

location ~ ^/files/(.*)\.(doc|txt|jpg)$ {
    alias /var/www/team_files/;
    try_files $1.$2 =404;

Now when my team enter the files directory, the team_files directory will be listed and all can download the files shared in that directory.

Run Apache and Nginx side by side

May 28th, 2015 § 0 comments § permalink

Recently I needed nginx and Apache side by side as one of our component was using nginx. Since now I am working on PHP, apache was my default server and it uses port 80. So when I installed nginx, it tries to use the same port 80 and failed to start. Now I need to run nginx in a different port other than 80.

So I decided to run nginx in port 8080. To change its default port, I updated the listen value in /etc/nginx/sites-enabled/default from 80 to 8080.

server {
        listen 8080 default_server;
        listen [::]:8080 default_server ipv6only=on;

After this change, I started running on port 8080 while my apache still running on 80. Thus I am able to run nginx and apache side by side.