In this article, we will provide a guide on configuring a technique to fake the IP of a localhost domain using the Nginx server for your website. The fake IP domain technique allows you to use the localhost IP, which is 127.0.0.1, to run your website. This can be beneficial for various purposes, such as:
- Testing your website before starting production.
- Experimenting with changes to your website without affecting direct user experience.
How to Use Localhost Fake IP Domain for a Website with Nginx
In the previous article, we provided a guide on install Nginx on Linux and gave an overview of the directories in Nginx. Now, let’s proceed to configure it for use on localhost.
By default when you add new domains and projects the configurations will be placed in /etc/nginx/sites-available and using the ln -s command creates a symlink to the /etc/nginx/sites-enabled directory to start. Here We will configure Nginx for the website to work both on localhost and Linux server and skip the ln -s symlink command step, which means you just need to add the domain configuration to /etc/nginx/sites-available then restart Nginx to be able to start the website.
Step 1. Custom configure the Main config for Nginx
Open the file /etc/nginx/nginx.conf
and add the following two lines at the end:
sudo nano /etc/nginx/nginx.conf
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-available/*.conf;
In the above configuration, we have included (included) two config files from the “conf.d” and “include” directories directly from the files in the sites-available directory. Therefore, you won’t need to use the ln -s
symlink command each time you create an Nginx configuration for a new domain or website. Our Nginx configuration file on localhost now looks like this:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
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;
ssl_protocols TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
client_max_body_size 128M;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-available/*.conf;
}
Step 2: Create a New Domain Configuration in Nginx
To add configuration, as you may already know, by default, all domain configuration files are stored in the /etc/nginx/sites-available directory. Now, let’s create a new domain here and add the Nginx configuration for your localhost.
sudo nano /etc/nginx/sites-available/arriveddev.com.conf
server {
listen 80;
listen [::]:80;
root /var/www/vhosts/arriveddev.com/httpdocs;
index index/ index.php;
server_name arriveddev.com www.arriveddev.com;
# Overrides logs defined in nginx.conf, allows per site logs.
access_log /var/log/nginx/arriveddev.com.vhosts.log;
error_log /var/log/nginx/arriveddev.com.vhosts.err;
location / {
try_files $uri $uri/ /index.php?$args;
}
}
- Listen: Default configuration for port 80 using HTTP.
- Root: Exact path to the directory containing the source code.
- Server_name: Domain or hostname.
- Access_log and Error_log: Paths to store log files.
Save the file and restart Nginx. If there is an error message, run the command “sudo nginx -t” to display detailed error information and fix it. You can also open the file “/var/log/nginx/arriveddev.com.vhost.err” to view the error details.
sudo service nginx restart
Now, simply copy your project source code into the previously configured directory. In this guide, we’ve set it as “httpdocs,” so our source code will reside within this directory.
Step 3. Initialize the domain and test the website
We are configuring the fake IP domain website localhost, so we need to assign the localhost IP, which is 127.0.0.1, to that domain. For example: 127.0.0.1 arriveddev.com
.
To do this, open the hosts file and assign the IP to your domain (Fake IP for the domain).
sudo nano /etc/hosts
These are the easy steps that we have shown on how to configure Nginx for a website on localhost Linux. You can also explore additional configurations with WordPress or Magento.