A web server is required to provide access to various services over a network. This post describes the process of setting up an Nginx web server, making it accessible within a local network.
Installing Nginx #
To install Nginx, execute the following command:
# Arch Linux
sudo pacman -S nginx-mainline
Configuring iptables #
To allow access to TCP ports 80 and 443, configure iptables as follows:
# Allow access to TCP port 80 from LAN (192.168.10.0/24)
iptables -A INPUT -p tcp -s 192.168.10.0/24 --dport 80 -j ACCEPT
# Allow access to TCP port 443 from LAN (192.168.10.0/24)
iptables -A INPUT -p tcp -s 192.168.10.0/24 --dport 443 -j ACCEPT
For further details, refer to: iptables Configuration.
Starting the Nginx Service #
Enable and start the Nginx service:
sudo systemctl enable nginx
sudo systemctl start nginx
Accessing http://[server-ip]
should display the default Nginx page.
Configuring nginx.conf #
Modify /etc/nginx/nginx.conf
as follows to disable direct IP access and enable virtual host configurations:
#user html;
worker_processes auto;
worker_cpu_affinity auto;
worker_priority 0;
#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 {
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 80 default_server;
server_name _;
return 444;
}
include sites-enabled/*;
Setting Up Virtual Hosts #
Creating Directories for Virtual Host Configuration #
The following directories should be created for managing virtual host configurations:
sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled
Placing Configuration Files in sites-available
#
An example configuration file for www.mkt3.dev
:
server {
listen 80;
server_name www.mkt3.dev;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Enabling a Virtual Host #
To enable the configuration:
cd /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/www.conf .
Configuring DNS #
To enforce domain-based access, DNS configuration is required. Modify the zone file with the following entry:
www IN A 192.168.10.xxx
For further dns details, refer to: Setting Up an Internal DNS Server Using NSD and Unbound.
Verifying Virtual Host Configuration #
Restart the Nginx service:
sudo systemctl restart nginx
Accessing http://[configured-domain]
should display the default Nginx page.