apache-http-webserver

Redirect site from www to non-www

It is better to make web site available with one URL. Many sites work with both wwww and non-www (naked domain) urls.

Using www or non-www is personal choice. One advantage of using wwww for URL is when you have lot of sub domains. If you use non-www url, cookies set by the domain will be available to sub domains. This will increase bandwidth usage as cookie need to be sent with every request browser make to web server.

Apache

If you are using Apache web server, you can redirect wwww to non-www url by adding following code in .htaccess file

Nginx

If you use Nginx, it is better create a server entry for www URL, then set a redirect

If you want to use same server entry for www and non-www, add following code to nginx server entry for the web site.

Redirect www domain to non-www

If you use custom ports, use

Redirect Naked Domain to www

Related Posts

Redirect

htaccess

Nginx Web Server

Show Real IP Nginx Behind Reverse Proxy

When your Nginx web server is running behind a reverse proxy, you will see IP of the reverse proxy server as visitor IP in web servers access log.

To fix this, edit nginx.conf file

Find

Inside http section, add

Example

Restart Nginx

Nginx Web Server

Install Nginx on CentOS 8

To install Nginx web server on CentOS 8, create repo

Add

Install nginx with dnf or yum

Open HTTP and HTTPS ports on firewall

Redirect HTTP to HTTPS when using Reverse Proxy

When you are using Reverse Proxy like Nginx, Haproxy or Amazon ELB in front of web server and web server use HTTP to serve all traffic, you can use normal redirect code based HTTPS variable to do the redirect to HTTPS. You need to use X-Forwarded-Proto to do the redirect.

For Apache, add following code to .htaccess to Apache Virtual Host entry.

For Nginx, add following to server entry for the domain name

For IIS edit web.config, add following to section.

Nginx Location Directive

Nginx Location Directive is used to route request to correct files.

Match

Exact match is used to match an exact URL.

When location is used with no modifiers, then beginning of the URL is matched. In this case, any url http://domain/ok/FILE_NAME will be served from /home/ok/FILE_NAME

Exact Match (=)

Exact match is used to match an exact URL.

In this example http://domain/ok/index.html get served from /home/ok/index.html. Only this specific file will be matched.

Cause Insensitive Regular Expression Match (~*)

Above code routes URL http://domain/ok/ to /home/ok/index.html. But won’t match http://domain/OK/.

If you need both /ok and /OK work, you need to use

With this config, http://domain/OK/FILE will be served from /home/OK/FILE.

See Nginx

nginx password protect

Nginx Password Protect a website

nginx password protect

To password protect a web site, you need to install htpasswd utility. On Ubuntu/Debian, you can install it with command

Now create a password file with command

It will ask for password.

Edit configuration file for your web site and add following in the server entry for the web site.

Restart Nginx.

Now on visiting the web site, you will be asked to enter username and password.

See Nginx

Nginx vs Apache

I recently added nginx as front end for apache. Now nginx serve static content, PHP requests are peroxided to Apache.

Nginx frontend, Apache backend

Apache Only

See Apache, Nginx

Configure Nginx to listen on single IP Address

By default Nginx listens on all IP address on a server. To make nginx listen on specific IP address, edit nginx configuration file

And VirtualHost/server files for each domain located in folders

Find

Replace with

IP_ADDR_HERE = your server IP address on which you need nginx listen on.

See Nginx

Running Python Application with gunicorn and nginx

Create a service file for gunicorn

Here

/home/ubuntu/myapp/wagtail2 = path to the folder where web application is.

/home/ubuntu/myapp/venv/bin/gunicorn = is where gunicorn installed inside virtualenv.

Change these path as required.

Restart gunicorn with

use following nginx config

Restart nginx

Nginx Web Server

Nginx Config for Laravel Application in sub folder

To run Laravel Application on sub folder of a web site, use following configuration. If you run Laravel application as main site, see Nginx Config for Laravel Application

Here you place Laravel application in a subdirectory “subFolderApp1”.

Example