Just switched back to Nginx

Several days ago I reinstalled my VPS with new Debian 8.0 x86-64 and oneinstack to get LAMP(Linux+Apache+Mysql+PHP) instead of LNMP(Linux+Nginx+Mysql+PHP) cause I found it a little hard to customize Error Pages like 404 and create redirects.

Normally with apache u just need to create .htaccess and insert some lines related to those functions and save the file. It's much easier to configure!

Things changed yesterday when I happened to see the newly installed system had used around 360 MB RAM, almost 100 MB higher than the highest used number of my previous LNMP at around 280 MB. What's more, the previous LNMP carried three Wordpress sites, in other words, three PHP+MYSQL sites while the newly installed LAMP carried two static HTML sites generated by Pelican. It looked really terrible as the number of RAM used still kept rising!

Searched and the things below seem to be the case:

LNMP or LAMP

From JqFrameWork

The most common software bundle is LAMP in the past. What is LAMP? Linux + Apache + MySQL + PHP

How about LNMP? it is almost the same, except the change of the web server from Apache to Nginx. Why there is such change? Loading and performance issue.

Apache will consume a lot of memeory, if you are using just very low-end vps, your vps maybe down due to all memory are being used. we have bad experinece in the past for OpenVZ, the apache eat up all memory and the machine shutdown and need reboot.

In recent year, Nginx become more popular, few benchmarks make comparison between both, Nginx seems a lot better on load balancing and memory consumption.

One of the disadvantage is that you could not use .htaccess anymore. You have to re-learn the syntax of Nginx to adapt your needs.

So today I just began backing up and then reinstalled the system and changed the system to Debian 7.0 x86 minimal (Cause x86-64 costs more RAM).

Then I reuploaded the things back and configured carefully and now it works like charm! (Since I haven't installed PHP and Mysql it has costed about 17 MB RAM, incredible! )

Ok, about configuration I had tested some codes and the codes below r what I used:

Custom Error Pages in Nginx

Thank the two sources: Source 1, Source 2

Configure single page

With the code inserted,the configuration file of ur site should look like below:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

. . .

error_page 404 /custom_404.html;
location = /custom_404.html {
root /usr/share/nginx/html;
internal;
}
}

The configuration code of 404 page is:

error_page 404 /custom_404.html;
location = /custom_404.html {
root /usr/share/nginx/html;
internal;
}

custom_404.html is the name of the 404 Page u customized which is located in /usr/share/nginx/html. So configure it to ur liking or need.
If u r not sure of the path why not use WinSCP to figure it out?

Configure Multiple Pages

Yep, u could also configure multiple types like the codes below:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

. . .

error_page 404 /custom_404.html;
location = /custom_404.html {
root /usr/share/nginx/html;
internal;
}

error_page 500 502 503 504 /custom_50x.html;
location = /custom_50x.html {
root /usr/share/nginx/html;
internal;
}
}

The configuration code of multiple error pages is:

error_page 500 502 503 504 /custom_50x.html;
location = /custom_50x.html {
root /usr/share/nginx/html;
internal;
}

Just edit /custom_50x.html and /usr/share/nginx/html to ur need. I just gave them definations above in Configure single page to make things clear. Personally I prefer the things like this:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

. . .

error_page 403 404 500 502 503 504 /error.html;
location = /error.html {
root /mypath;
internal;
}
}

Yep, it's the code below that configure 403,404,500,502,503,504 pages to one page:

error_page 403 404 500 502 503 504 /error.html;
location = /error.html {
root /mypath;
internal;
}

After configuration

Yep that's the point where I stopped before, without which the codes won't be put into effect. Save the file u edited above and use the command to reload Nginx:

service nginx restart

Create Redirects in Nginx

Thank the source and one source(I just couldn't remember sorry) telling me if is not recommanded.

For example, they r the redirects I wanna create:

http://x.xx/issue1 --> http://x.xx/shop/issues/custom_issue_name1
http://x.xx/issue2 --> http://x.xx/shop/issues/custom_issue_name2
http://x.xx/issue3 --> http://x.xx/shop/issues/custom_issue_name3
...

The solution is quite simple and I'm using the second:

For multiple redirects - Way 1 - Variables

Put this in my server directive in the configuration file of my site:

location /issue {
rewrite ^/issue(.*) http://$server_name/shop/issues/custom_issue_name$1 permanent;
}

For multiple redirects - Way 2 - Duplicate

I could also duplicate it in my server directive in the configuration file of my site:

location /issue1 {
rewrite ^/.* http://$server_name/shop/issues/custom_issue_name1 permanent;
}

location /issue2 {
rewrite ^.* http://$server_name/shop/issues/custom_issue_name2 permanent;
}
...

Besides, earlier I painted a Snorlax face and now it's become my new avatar!

Though imperfect, it's meaningful for me!

BTW, if u wanna use <pre> with colored,underlined or any other-styled code inside, use .html instead of .md to make a post.

In markdown, * inside the <pre>...</pre> or any other html elements would just be interpreted into italic words(<em>...</em> in html code) when there's another * behind.

So use TAB in markdown to make a <pre>...</pre>