Nginx is a very good Web servers which is fast and highly customizable. Following are the few redirects used in daily work.
A simple vhost with HTTP and HTTPS configuration.
server_name : For domain name to be hosted.
root : Its the location of sites document directory.
index : For pages which need to be rendered first.
access_log & error_log : for sites logs.
include : To include any file in vhost.
A simple redirecting vhost.
To allow specific files to miss the cache and fetch content from server following rule can be helpful.
To add specific headers to all the files.
Redirect everything followed by character or directory.
Redirect only a specific character to other.
Redirect multiple URL's to single URL.
Redirect passing the relative path to redirected URL.
Redirect all HTTP requests to HTTPS.
A simple vhost with HTTP and HTTPS configuration.
server {
listen 80;
server_name testsite.domain.com;
root /data/html/;
index index.html index.htm;
access_log /var/log/nginx/testsite.domain.com_access.log;
error_log /var/log/nginx/testsite.domain.com_error.log;
include /etc/nginx/denyhost.conf;
}
server {
listen 443 ssl;
server_name testsite.domain.com;
root /data/html/;
index index.html index.htm;
access_log /var/log/nginx/testsite.domain.com_access.log;
error_log /var/log/nginx/testsite.domain.com_error.log;
include /etc/nginx/denyhost.conf;
}
listen : Its for port on which you want web server to listen, 80 for HTTP and 443 for HTTPS.server_name : For domain name to be hosted.
root : Its the location of sites document directory.
index : For pages which need to be rendered first.
access_log & error_log : for sites logs.
include : To include any file in vhost.
A simple redirecting vhost.
server {
listen 80;
server_name support.domain.com;
return 301 https://support.zendesk.com/hc/en-us;
}
server {
listen 443;
server_name support.domain.com;
return 301 https://support.zendesk.com/hc/en-us;
}
This will redirect all the requests coming to support.domain.com to https://support.zendesk.com/hc/en-us.To allow specific files to miss the cache and fetch content from server following rule can be helpful.
####To solve the font awesome icon issue on FireFox and IE
location ~* \.(eot|otf|ttf|woff|woff2)$ {
add_header Access-Control-Allow-Origin *;
}
To add specific headers to all the files.
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
Redirect everything followed by character or directory.
location /a {
rewrite ^/a /b/ permanent;
}
It will redirect everything followed by /a to /b/. For example /a, /abc, /aabb/ etc.Redirect only a specific character to other.
location = /a {
return 301 /b/;
}
It will only redirect the /a to /b/, anything followed /a will not be redirected.Redirect multiple URL's to single URL.
location ~ ^/(sign-up|sign-in) {
return 301 https://domain.com;
}
It will redirect the sign-up and sign-in to https://domain.com.Redirect passing the relative path to redirected URL.
rewrite ^/ab/(.*) http://www.domain.com/$1 permanent;
It will pass the exact path after /ab/ to the destination URL. Redirect all HTTP requests to HTTPS.
###To redirect requests from http to https
if ($http_x_forwarded_proto != 'https') {
rewrite ^(.*) https://$host$1 permanent;
}