Skip to content

Instantly share code, notes, and snippets.

@sayanriju
Last active March 17, 2023 13:24
Show Gist options
  • Save sayanriju/377a573d0c6bf87018222628d471ceac to your computer and use it in GitHub Desktop.
Save sayanriju/377a573d0c6bf87018222628d471ceac to your computer and use it in GitHub Desktop.
Web Server Configs

Minimal VirtualHost Config

Nginx

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

       server_name example.com www.example.com;

       root /var/www/html/;
       index index.html;

       location / {
               try_files $uri $uri/ =404;
#               try_files $uri /index.html =404;
       }
       
       ### OPTIONAL: Always Redirect http to https ###
       # return 301 https://example.com$request_uri;
}

Apache

<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

 	<Directory /var/www/html> 			
            Options Indexes FollowSymLinks
            DirectoryIndex index.html index.php
            AllowOverride All
            Require all granted
            ## Allow from all
      </Directory>
      
      ### OPTIONAL: Always Redirect http to https ###
      # RewriteEngine on
      # RewriteCond %{SERVER_NAME} =example.com
      # RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>


Reverse Proxy setup

Nginx

server {
    listen    80;
    server_name example.org www.example.org;

    location / {
        proxy_pass http://localhost:3000;
        proxy_redirect off;
        proxy_http_version 1.1;
        
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
                
        # WebSocket support (nginx 1.4)
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Apache

<VirtualHost *:80>
    ServerName example.org
    ServerAlias www.example.org
    
    ProxyPreserveHost On

    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
</VirtualHost>
@sayanriju
Copy link
Author

server {
  server_name example.com;

  root /home/ls/app/;
  index index.html;

  location / {
          try_files $uri $uri/ =404;
#             try_files $uri /index.html =404;
  }

  listen [::]:443 ssl ipv6only=on; # managed by Certbot
  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/example.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 = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;

    server_name example.com;
    return 404; # managed by Certbot

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment