Score:0

Nginx server keeps serving Debian main page

in flag

I am trying to set up a simple custom website. I have built a simple server using Golang and the Gin-gonic framework. My Go server is as follows:

func main() {
        r := gin.Default()
        r.GET("/", ServeMainPage)
        r.GET("/icons/openlogo-75.png", func(c *gin.Context) {
                c.File("favicon.ico")
        })
        r.GET("/episode/:id", ServeEpisode)
        r.GET("styles/style.css", func(c *gin.Context) {
                c.File("styles/style.css")
        })
        r.GET("scripts/main.js", func(c *gin.Context) {
                c.File("scripts/main.js")
        })
        r.GET("favicon.ico", func(c *gin.Context) {
                c.File("favicon.ico")
        })
        r.Run()
}

func ServeMainPage(c *gin.Context) {
        c.File("index.html")
}

func ServeEpisode(c *gin.Context) {
        episodeID := c.Param("id")
        filepath := "AudioFiles/" + episodeID + ".mp3"
        c.File(filepath)
}

Because this process cannot access port 80 (it runs on :8080), and because I am trying to develop my skills, I have attempted to set-up an nginx server to act as a reverse proxy. My nginx server has the following conf, which is the default,

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

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

and I followed the tutorial here to have it forward things to my Golang app by adding the following to /etc/nginx/sites-available/ro and symbolic linking that to /etc/nginx/sites-enabled/ro.

server {
        listen 80;
        server_name running-oaks.com;

        location / {
                proxy_set_header   X-Forwarded-For $remote_addr;
                proxy_set_header   Host $http_host;
                proxy_pass         "http://127.0.0.1:8080";
        }
}

If I use curl on my local PC to access my server, I get the index.html page I would like it to server. My curl command is simply curl http://123.123.123.123, where 123.123.123.123 is replaced by the actual IP of my remote server.

However attempting to access this same IP address with my web browser simply shows me the default Apache for Debian page. I uninstalled apache before setting up nginx in order to hopefully avoid conflicts. In both the curl case and the browser case, I can see that my Golang app is receiving the requests and returning a status 200.

So why does my browser keep showing me the debian page?

I have also changed /var/www/index.html to be the default nginx page, although that hasn't fixed it either.

Score:0
de flag

The problem is that your request doesn't match your server block, but does match the default server block.

You have:

server {
        listen 80;
        server_name running-oaks.com;

Which will match any request coming in on port 80 with the Hostname "running-oaks.com".

Out of the box, Ubuntu includes an already enabled site file /etc/nginx/sites-enabled/default which is a symlink to /etc/nginx/sites-available/default. This is the site definition that provides the Debian main page.

In this config file, there's a server block that starts like this:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

This block has no server_name, but does have the default_server directive. This means that any request coming on port 80 that doesn't match another server block should use this server block.

Since your request is using the direct IP and not the hostname that you specified on the server_name directive, your request matches the default server instead of your own.

To fix this, you could delete the symlink at /etc/nginx/sites-enabled/default and add the default_server directive to your listen line. Remember to restart Nginx after doing this.

(Adding the default_server is redundant because if there is only one server it defaults to being the default, but it's better to be explicit.)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.