NGINX server_name malfunctioning

Whenever I try to access my website, it always opens with the localhost path instead of the correct domain name specified in my server configuration. How can I resolve this issue?

View Configuration Image

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {

    server {
        listen 80;
        server_name  mydomain;

        #charset koi8-r;

        access_log  logs/host.access.log;

        location / {
            proxy_pass http://127.0.0.1:3037;
        }

    }

}

Answer №1

Update your configuration as shown below

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {

    server {
        listen 80 default_server;
        return 403;
    }

    server {
        listen 80;
        server_name  example.com;

        #charset koi8-r;

        access_log  logs/host.access.log;

        location / {
            proxy_pass http://127.0.0.1:3037;
        }

    }

}

The first server block acts as the default server for requests in case no virtual host matches. Therefore, it is necessary to have two blocks if you want specific server_name to be allowed while denying the rest.

Answer №2

If you want to test and accept doing a "catch-all", simply utilize the code server_name _

Source:

In examples of catch-all servers, you may notice the unconventional name “_”:

server {
    listen       80 default_server;
    server_name  _;
    return       444; 
}

Answer №3

In order to use Ubuntu, it is necessary to specify your server name for your local IP address in the /etc/hosts file like this:

127.0.0.1 mydomain www.mydomain.com mydomain.com

Answer №4

In order to link your unique domain name to the specific local IP address of your device, you must ensure that they are in sync. This can be achieved by either using the default 127.0.0.1 setting or executing the "ip addr" command within your Ubuntu terminal. By running this command, you will receive a list containing two available IP addresses associated with your machine. Once these IP addresses are obtained, you can then align one of them with your custom domain by updating the "/etc/hosts" file.

Answer №5

Here is a solution: Include the bad URLs in your "/etc/hosts" file, similar to the example below: view image description here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Calling Node.js middleware externally results in bypassing its functionality

As a newcomer to Express and Node, I'm facing what seems like a very basic issue that is causing me quite a bit of confusion. Any assistance in resolving this problem or guiding me on how to troubleshoot it would be greatly appreciated. Issue var ...

Sharing data between ejs and javascript files

When using Express, I have encountered an issue with passing a variable to an EJS file called "index.ejs". res.render("index",{passedUser:req.user.alias}) Although I am able to successfully print it using <%=passedUser%> in the EJS file, I require ...

Establishing a user session with Node.js

I am new to the world of node.js and JavaScript in general. I have a piece of code that currently handles login functionality by checking if a user exists in a MYSQL database. This part is functioning correctly. Now, I wish to improve this feature by crea ...

Issue with express-validator returning undefined value on forms set to enctype='multipart/form-data'

Currently, I am developing a login authentication application using basic node.js+express. While extracting values (such as name, email, etc) from the registration page, I utilize express-validator for validation. However, I encounter an issue where all va ...

Deployment of the website resulted in a NextJS 500 internal server error, yet the build functions flawlessly when tested locally

Everything runs flawlessly on my personal computer using pm2. There are no errors, every page loads perfectly, and fetching files does not result in any 404 or 500 errors. It's absolutely fantastic! This is exactly how I envision it working. However, ...

Saving numerous files with Promises

There is a Node URL (created using Express) that enables users to download static images of addresses. The calling application sends a request to the /download URL with multiple addresses in JSON format. The download service then calls Google Maps to save ...

What is the process by which node.js synchronously delivers a response to a REST web service?

Sorry if this question seems obvious! I'm struggling to grasp how node.js handles requests from the browser. I've looked at tutorials online where express.js was used to create the server-side code with node.js. Routes were then set up using prom ...

The absence of the 'Access-Control-Allow-Origin' header is detected in the requested resource by Keycloak

We are currently experiencing an issue with accessing our nodejs app from Chrome, which has Keycloak configured. Keycloak version: 21.0.1 When trying to access http://localhost:3101/graphql from Chrome, we encountered the following error in the browser c ...

Performing a MySql query to retrieve data from two tables often leads to a high number of redundant entries

I'm currently utilizing the latest version 8.* of MySQL from Oracle. In my setup, I am using node.js in conjunction with express and have multiple tables that share the same structure involving an auto_increment id and some columns. For the index page ...

Uploading CSV or text files in Node.js

I am looking to develop a function that can upload CSV or txt files into MongoDB using the MEAN stack. The function should work by allowing me to upload a file, then it will verify if it is in text/csv format before uploading it to MongoDB. I have attemp ...

Guide on sending JSON data as a key parameter using res.render()

Hello, I am a beginner with Node.js and I'm currently attempting to send JSON data to index.pug for rendering. The JSON file is located in the root directory, while the index.pug file that receives the data is within a views folder. This JSON data con ...

Is database connection pooling supported in this custom ORM approach for NodeJS?

I created a file named pool.js to establish the database connection pool in the following way import mysql from 'mysql2'; import dotenv from 'dotenv'; dotenv.config(); const pool = mysql.createPool({ host: process.env.DB_HOST, ...

Combining collections using the "_id" field in MongoDB: A step-by-step guide

Greetings, I am currently in the process of merging two collections. Products Categories The only connection between them is the ObjectId of the corresponding document. Take a look: PRODUCT COLLECTION { "_id": Object(607a858c2db9a42d1870270f), ...

`Express.js Controllers: The Key to Context Binding`

I'm currently working on a project in Express.js that involves a UserController class with methods like getAllUsers and findUserById. When using these methods in my User router, I have to bind each method when creating an instance of the UserControlle ...

Is there a way to modify the URL in a Node.js request using JavaScript?

Is there a way that I can modify the API 'keyword' parameter to show different results each time it is accessed? Specifically, I am looking to achieve this with the following endpoint: http://localhost:3009/api/get-products/?keywords=naruto. ...

What is the best way to print a MongoDB database query to the console instead of the mongo shell?

I want to output the results of a MongoDB database query using console.log() without accessing the mongo shell. Check out my code below, with the important parts highlighted: router.put('/quotes/:id', (req, res, next) => { let personToUpdat ...

Is it possible to include a parameter in module.exports?

A module in my application runs a query and uses express to display the results. module.exports.runQuery =function(req,res){ //establishing connection connection.on('connect', function(err) { console.log("success"); //if connec ...

Struggle encountered while incorporating PayPal into my project

Recently, I encountered an issue while trying to integrate the PayPal-node-SDK into my project for a Pay with PayPal option. The error message I received was: XMLHttpRequest cannot load https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&a ...

The npm package installation process encountered difficulties in accessing the Chart.Js library

Currently, I am in the process of developing a web application that tracks and logs hours spent on various skills or activities. The data is then presented to the user through a bar graph created using Chart.js. Initially, I was able to display a mock grap ...

"The loop functionality appears to be malfunctioning within the context of a node

Below is the code snippet: for (var j in albums){ var album_images = albums[j].images var l = album_images.length for ( var i = 0; i < l; i++ ) { var image = album_images[i] Like.findOne({imageID : image._id, userIDs:user ...