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 attend Red Hat Remote Exam ? Every details you need to know

In August 2020, Red Hat announced the availability of Remote exams for students and the entire Learning community were so happy, yes we can attend Red Hat exams from our home or offic...

In redhat, Nov 09, 2020
How to Import Existing VMWare VM’s into Terraform

Terraform is an amazing tool for your infrastructure automation. Everything about your infrastructure can be write as code and maintain by team; means your infrastructure is transpare...

In terraform, Nov 04, 2020
Connecting Ansible Tower to Git Server with Self Signed Certificates

So many questioned me when I mention git server in an Ansible Tower environment; and later I realized that, most of them are keeping their projects inside Ansible Tower !!!

In Ansible, Oct 12, 2020
HashiCorp Certified Terraform Associate – Learning & Exam Tips

I started using Terraform somewhere in 2018, but very limited usage as I thought it is just another tool for provisioning infrastructure and other services – or a variant of Vagrant; ...

In terraform, Sep 18, 2020
How to Pass CKA & CKAD Exams ? 10 Tips for Kubernetes Exams

Here see the best tips for Kubernetes Exams - Certified Kubernetes Administrator (CKA) & Certified Kubernetes Application Developer (CKAD).

In kubernetes, Aug 15, 2020

Featured