August 17, 2024
Nginx Domain Mapping And Configuration
1. Create a domain mapping file, in folder /etc/nginx/, default file is for primary/main domain. We can create config file with domain name. Config file sample mentioned below:
Configuration file (name: default) without ssl certificate
server {
listen 80;
listen [::]:80;
server_name
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location / {
proxy_pass http://(localIP):3000;
}
}
Configuration file (name: default) with ssl certificate
server {
listen 80;
listen [::]:80; server_name ;
return 301 http://$server_name$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name <>;
ssl on;
ssl_certificate /etc/ssl/certs/certificate.crt;
ssl_certificate_key /etc/ssl/certs/private.key;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.2:3000;
}
}
2. Copy config files from folder /etc/nginx/ to folder /etc/nginx/sites-available with below commands
$ sudo cp default sites-available/
3. To enable sites/site’s congituation, create soft links with below commands
$ sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
4. Reload nginx service to implement/activate all configurations to system with below command
$ sudo /etc/init.d/nginx reload
Test all configuration is ok or not with below command:
$ sudo nginx -t
Note: all ssl files (i.e. private.key, certificate.crt, ca_bundle.crt) need to be stored on /etc/ssl/certs/ folder