Setup a File Server - hqzhang/cloudtestbed GitHub Wiki

Config nginx Forward/Reverse Proxy

////
1) run goserver
#! /usr/bin/env gorun
package main
import (
        "fmt"
        "net/http"
)
func main() {
        http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
                fmt.Fprintf(w, "Welcome to my website!\n")
        })
        fs := http.FileServer(http.Dir("static/"))
        http.Handle("/static/", http.StripPrefix("/static/", fs))
        http.ListenAndServe(":8222", nil)
}

////
2)change nginx config and run
vim /usr/local/etc/nginx/nginx.conf
#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;

    server {
        listen       9090;
        server_name  localhost;
        #access_log  logs/host.access.log  main;

#Forward Proxy #setting proxy(forward) by configure chrome with proxy 127.0.0.1:9090

        location / {
            resolver 8.8.8.8; # may or may not be necessary.
            proxy_pass http://$http_host$uri$is_args$args;
        }
    #setting reverse proxy using curl localhost:9090/reverse
        location /reverse {
            proxy_pass   http://127.0.0.1:8222;
        }
    #setting common file server
        location /common {
            root   html;
            index  index.html index.htm;
            autoindex on;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    }
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
3)sudo ngnix -s stop; sudo nginx
4)testing
curl localhost:9090/reverse    for reverse proxy
curl localhost:9090/common .   for regular server
chrome brawser setting proxy: 127.0.0.1:9090
and input www.google.com on chrome  for forward proxy

Config file server

File server is a good way to shared file to other. One can download a file use wget or curl to down a file after setup the server.
1. install nginx or appach2.
   sudo apt install nginx
   sudo apt install apache2

2. For apache2, create directory for files
   mkdir /var/www/html/mydir/
   notice: apache2 config file tuple below: DocumentRoot /var/www/html

3. For nginx, create directory for files
   mkdir /usr/share/nginx/html/mydir/
   notice:root /usr/share/nginx/html in ngnix config file below.

4. put file test.txt into directory mydir and then
   view: http://localhost:80/mydir
   download wget http://localhost:80/mydir/text.txt

set nginx config file

   1) main config file:  /etc/nginx/ngin.conf
   http {
        ........
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/default
        }
    2) subconfig file: /etc/nginx/sites-enabled/default
    server {
       listen 8000 default_server;
        listen [::]:8000 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;
        autoindex on;
        ......
   }
3) sudo nginx -s stop; sudo nginx
4) testing
i) curl localhost:9090/common
ii) curl localhost:9090/forward
iii) brawse proxy setting 127.0.0.1:9090 and then input
     www.google.com
  1. apache2 config file
 1) main config file: /etc/apache2/apache2.conf
   IncludeOptional conf-enabled/*.conf
   # Include the virtual host configurations:
   IncludeOptional sites-enabled/*.conf
   # Include the modules installed
   IncludeOptional mods-enabled/*.conf
   command: a2enconf  a2enmod a2ensite 
   Copy stuff from dir: xxxx-available into dir: xxxx-enabled
 2) include file: /etc/apache2/site-enableed/000-default.conf
 <VirtualHost *:80>
      ServerAdmin webmaster@localhost
      DocumentRoot /var/www/html
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>