web,

nginx configuration samples

Gini Gini Follow · 1 min read
nginx configuration samples
Share this

Basic website configuration

server {
    listen 8080;
    listen [::]:8080;

    server_name example.com;
    root /var/www/example;
    index index.php index.html index.htm;
    access_log /var/log/nginx/access_example.log;
    error_log /var/log/nginx/error_example.log;
}

nginx proxy_pass - site2.com -> site1.com/site2-page

Scenarios

  • nginx and wordpress - site2.com need to load content of of site1.com/site2 without url change
  • show a landing page for subdomain (sub.site1.com) by using site1.com/sub-page
  • How to Redirect a Domain without Changing the URL on browser.

Sample nginx configuration file for site2.com

server {    
    index index.php index.html index.htm;

    server_name site2.com www.site2.com;

    access_log /var/log/nginx/access_site2.log;
    error_log /var/log/nginx/error_site2.log;

    location / {
        proxy_pass https://www.site1.com/site2/;
        proxy_ssl_server_name on;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/site2.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/site2.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = www.site2.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = site2.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;
    server_name site2.com www.site2.com;
    return 404; # managed by Certbot
}

Basic Reverse Proxy Configuration

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

  access_log /var/log/nginx/reverse-access.log;
  error_log /var/log/nginx/reverse-error.log;

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

(Photo by luis gomes from Pexels)

References

Gini
Written by Gini Follow
Backpacker, Foodie, Techie

Latest Stories

How To Get Hands-On Experience in AWS

Every Time you have the same situation, you know the technology and maybe you are already a certified professional in that technology, but you are not getting enough hands-on on that ...

In AWS, Jan 04, 2021
Getting Started with Ansible Collections

Ansible Collection is a great way of getting content contributions from various Ansible Developers. Earlier there was a tagline for Ansible – “Batteries included”, but now the battery...

In redhat, ansible, Dec 22, 2020
Configure Your Windows Host to be Managed by Ansible

I was talking to my friend about Ansible automation and how we are implementing automated solutions for cloud and on-premise infrastructure. Then he told me that, his team is looking ...

In redhat, ansible, Dec 01, 2020
Remove nodes from Kubespray Managed Kubernetes Cluster

Kubespray is a combination of Ansible and Kubernetes and you can use Kubespray for deploying production ready Kubernetes clusters. You can manage full-lifecycle of Kubernetes clusters...

In containers, kubernetes, Nov 30, 2020
Adding new nodes to Kubespray Managed Kubernetes Cluster

Kubespray is a combination of Ansible and Kubernetes and you can use Kubespray for deploying production ready Kubernetes clusters. Learn how to add new nodes in a Kubernetes cluster u...

In containers, kubernetes, Nov 27, 2020

Featured