Install Nginx web server to share files and folders (display file content instead of downloading)

Install Nginx web server to share files and folders (display file content instead of downloading)

The goal of this configuration is to install Ngnix in CentOS 7.5 and setup to act as a simple file/folder sharing. By default when file is clicked it is downloaded but we want to have display file content.

1. Install NGNIX

Login to CentOS (in this example as root user) and install Nginx web server:

# yum install nginx

During installing accept all dependencies to be installed and here is the final resul:

Installed:
  nginx.x86_64 1:1.12.2-3.el7                                                                                                                                                                       

Dependency Installed:
  dejavu-fonts-common.noarch 0:2.33-6.el7     dejavu-sans-fonts.noarch 0:2.33-6.el7              fontconfig.x86_64 0:2.13.0-4.3.el7           fontpackages-filesystem.noarch 0:1.44-8.el7        
  gd.x86_64 0:2.0.35-26.el7                   libX11.x86_64 0:1.6.5-2.el7                        libX11-common.noarch 0:1.6.5-2.el7           libXau.x86_64 0:1.0.8-2.1.el7                      
  libXpm.x86_64 0:3.5.12-1.el7                libjpeg-turbo.x86_64 0:1.2.90-6.el7                libpng.x86_64 2:1.5.13-7.el7_2               libxcb.x86_64 0:1.13-1.el7                         
  nginx-all-modules.noarch 1:1.12.2-3.el7     nginx-filesystem.noarch 1:1.12.2-3.el7             nginx-mod-http-geoip.x86_64 1:1.12.2-3.el7   nginx-mod-http-image-filter.x86_64 1:1.12.2-3.el7  
  nginx-mod-http-perl.x86_64 1:1.12.2-3.el7   nginx-mod-http-xslt-filter.x86_64 1:1.12.2-3.el7   nginx-mod-mail.x86_64 1:1.12.2-3.el7         nginx-mod-stream.x86_64 1:1.12.2-3.el7             

Dependency Updated:
  freetype.x86_64 0:2.8-12.el7_6.1                                                                                                                                                                  

Complete!

2. Enable and start Nginx service

Enable Ngnix service:

# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

Start Ngnix Service:

# systemctl start nginx

Test if the Nginx web server is working by entering CentOS address:

3. Configure Ngnix to share files and folders

To share files and folders first create destination folder for shared files and folders:

# mkdir /usr/share/nginx/files

Update Nginx configuration files with the following changes:

root         /usr/share/nginx/files;
autoindex on;

Full config:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/files;
        autoindex on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {

Open the browser and enter IP address of the Nginx server:

4. Configure Nginx to display file content

After this change if you click on the file it will be downloaded. To have content file displayed add the following lines in the “location” section:

location / {
                types {}
                default_type text/plain;
        }

Final result:

Supported (tested) browsers:

  • Safari
  • Firefox
  • Opera

Unsupported:

  • Chrome

Leave a Reply