August 2017
Intermediate to advanced
278 pages
8h 53m
English
If you're used to the Apache way of doing things, this may look a little different. In the NGINX world, if is evil and needs to be avoided at all costs. Instead, we set one server block up for the non-www site and set it to redirect and a separate server block for the www-based site. Here's an example code:
server {
listen 80;
server_name redirect.nginxcookbook.com;
return 301 http://www.redirect.nginxcookbook.com$request_uri;
}
server {
listen 80;
server_name www.redirect.nginxcookbook.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
Using the Chrome DevTools (or the equivalent), we can see the following sequence:
The original request to redirect.nginxcookbook.com sends a 301 to immediately ...
Read now
Unlock full access