|

THE best oil for natural hair. A mega summary! – For Long, Healthy Natural Kinky and Curly Hair – Your Dry Hair Days Are Over!
Sesame Oil
Sesame oil is a light, nutritious oil obtained from sesame seeds. It has got lots of uses and health benefits. Sesame oil may not be the most popular oil in our times, but it was the most sought after oil in Ancient India. One of the earliest and profound writers on Ayurveda, Charaka in his Charaka Samhita mentions that sesame oil is the best oil. It was widely used back then for alleviating the body from various diseases and conditions. That is perhaps the oldest reference to this oil. Only now are we beginning to realize the strong health benefits of sesame seed oil.
Sesame oil can help one achieve flawless skin, healthy hair and radiant health from the inside out. Here, we look at the medicinal qualities of sesame oil and its nutritional value.
Source
Sesame oil comes from sesame seeds. These are the seeds obtained from a variety of species in the broad genus Sesamum. The plants in this genus have a special ability. They tolerate drought very well. People of the ancient civilizations in India, China, Egypt and in Middle East discovered that sesame seeds are good for producing oil. Sesame oil is thus one of the oldest extracted oils in the history of human civilization.
Sesame seeds are mainly of two types –
•White sesame seeds – These are white or creamy in color. Oil obtained from these sesame seeds has a lighter color.
•Black sesame seeds – These are dark in color, and the oil obtained from these seeds is dark in color.
There are other colors of sesame seeds as well, like yellow and red.
There are many varieties of processed and adulterated sesame seed oil in the market. One should be wary of such oils. A good idea is to always go for cold pressed virgin, or extra virgin sesame seed oil, whether you want to use it in recipes or as massage oil.
Properties
Sesame oil has got many powerful therapeutic and medicinal properties. The source of these properties are often taken from ancient medicinal texts, like Charaka Samhita, Susruta Samhita and Ebers Papyrus ( famous ancient papyrus scroll from Ancient Egyptian civilization ). Few of these properties have been evaluated and verified through scientific research.
•Anti-inflammatory – it aids in reducing inflammation because of healthy fatty acids in it.
•Antioxidant – Sesame oil contains many antioxidants. It does not spoil quickly because of these antioxidant nutrients.
•Anti-diabetic – relieves diabetes.
•Hypotensive – lowers blood pressure.
•Anti-bacterial – kills many strains of bacteria.
•Anti-atherogenic – reduces the risk of atherosclerosis
•Anti-depressant – lowers the state of depression.
•DNA protection – sesame oil safeguards the DNA of body cells from damage by external factors, like radiation.
•Anti-cancer – sesame oil has now been identified to protect and inhibit the growth of specific cancers, as we shall see later.
•Emollient – excellent moisturizer, heals dry skin.
•Anti-aging – sesame oil has powerful anti-aging nutrients !
•Skin regeneration – promotes repair and regrowth of skin cells.
•Cicatrizant – moderate wound healing property.
•Sunscreen – sesame oil has natural SPF, although mild. It doesn’t compare to sunblocks, but it can be used in natural sunscreen formulations.
•Anti-rheumatic – used in Ayurvedic oil formulations as base oil to infuse herbs. These are used to treat rheumatoid arthritis.
•Anti-cholesterol – improves lipid profile of people who use it.
•Detoxifying – one of the few massage oils known to remove toxins from the body.
•Warming – creates a warming sensation upon massage.
•Laxative – quickly provides relief from constipation.
You can’t help but ponder that ancient people revered this oil because of its healing properties. There are many more of its uses and properties mentioned in those books, but they have become difficult to decipher and understand. Hopefully, in due course of time, we can get to know many more beneficial medicinal properties of sesame oil and bring it back to its glory.
Uses and Health Benefits
We have seen the properties of this oil, now let’s put it to use. These are the ways in which one can use sesame seed oil at home, cold pressed of course.
1. Sesame Oil For Face Care
Sesame oil should be applied directly to the facial skin. It improves the skin texture, making it softer and smoother. Because of its emollient properties, it moisturizes the skin. Applying a small amount of sesame seed oil can reduce the appearance of wrinkles and fine lines. This effect is because of a special micronutrient called sesamol. It can also lighten sun spots and irregular pigmentation. Because of its antioxidant and sunscreen properties, it provides double protection from UV – B induced skin damage.
2. As Massage Oil
Sesame oil, despite being slightly heavier than other massage oils, can still be used for a massage. It is preferable used for detoxifying the body through the skin. Here is how to correctly massage using sesame oil.
Warm sesame oil till it is just as warm as tepid water. Now apply it in copious amounts on the skin. The idea is to bathe the skin in this oil. Massage it firmly into the skin. Keep the oil on the skin for about 15 – 30 minutes. Wash the oil down with water. This is ideal for an early morning massage before the shower, although one can use it any time.
This massage takes away many environmental toxins and internal toxins ( released by the liver to the skin ), leaving the body detoxified. In fact, skin is the largest organ on the body and a prominent means of releasing toxins for the body. On the contrary, we absorb toxins from the skin, many of which are trapped inside the skin. This massage also improves the blood circulation of the skin, making the skin perform better.
According to Ayurveda, sesame oil is to used daily in the morning for a self massage , called Abhyanga . This massage is aimed to strengthen the tissues of the body, reduce physical stress, improve the appearance of skin and protect from advancing old age. Sesame is renowned for its ability to neutralize “Vata” disorders.
Infant Massage – Although the dominant oils for infant massage are olive oil and mustard oil. But, sesame oil too can be used for this purpose.This study [1] found out that applying sesame oil on baby’s skin promotes growth in them. Babies massaged with sesame oil registered an added increase in height, weight and circumference of their arms, although serum levels of nutrients remained the same. It also boosted sleep after massage.
3. Sesame Oil For Hair Growth
Just as for skin, sesame oil makes a great massage for scalp. It deep conditions the scalp and improves the health of hair follicles. These are the health benefits of sesame oil for hair health.
•Sesame oil treats damaged hair. Our hair gets damaged due to use of chemicals, like from soaps, shampoo, conditioners, hair colors and even from high heat of the rollers and straighteners. Sesame oil returns hair back to its original health ( to some extent ). It dissolves away many toxins that accumulate on the scalp. It can also dissolve many oil soluble vitamins and minerals which may have deposited on the scalp. This usually happens because of bathing in sea water or water that has TDS ( total dissolved solids ).
•Sesame oil hot oil treatment – Apply warm sesame oil on the scalp as part of hot oil treatment. This makes hair softer and detangles them. Hot oil treatment is done for deep conditioning.
•Provides nutrition to hair follicles – Sesame oil has a good essential fatty acid profile which boosts health of hair follicles.
•Scalp psoriasis – Sesame oil help in scalp psoriasis as it eliminates flakiness and dryness. That is why it also combats dandruff.
•May prevent hair loss – Sesame oil may be useful in preventing hair loss that occurs because of a fungal scalp infection. Sesame oil is a potent anti-fungal and it kills the fungi to keep scalp fungi free.
•Quick shine – Going out and need some shine to dull hair ? No problem. Just take 4 – 5 drops of sesame in your palm and run them through hair. Enjoy the shine. Massaging at the ends gets rid of split ends.
•Get rid of lice – Add few drops of lavender oil and rosemary oil to sesame oil and apply as hot oil treatment on scalp. Lice is gone. Use a nit comb to extract their eggs.
4. Calm depression
Sesame oil , when taken internally or as massage helps to manage depression better. Research has identified that sesamol from sesame oil helps inhibits the production of cytokines ( like TNF – ? ) [2]. This reduces the risk of developing clinical depression because of stress.
5. Oil Pulling with Sesame Oil
One may be familiar with the ancient practice of oil pulling. For those f you who don’t know, oil pulling is the act of swishing oil inside the mouth. Commonly, coconut oil is popular for this practice. Sesame oil can also be used for oil pulling.
Take a mouthwash amount of sesame oil. Swish it inside the mouth for about 5 minutes. It kills bad bacteria in the mouth, fungi species like candida that cause oral thrush, protects teeth and gums. This practice is also helpful in gingivitis. [3]
6. Better Intestinal Health
Sesame oil helps to improve intestinal health. Regular consumption of sesame oil in diet may improve certain inflammatory conditions of the alimentary canal, like IBS, IBD, Ulcerative Colitis and Crohn’s disease. It is also helpful in treating intestinal obstruction which leads to fecal smelling vomiting. [4]
7. Maintain a health cholesterol level
Because of the presence of healthy fatty acids, sesame oil can reduce LDL cholesterol all the while keeping HDL cholesterol up. Since concentration of HDL cholesterol ( good cholesterol ) increases, the body becomes healthier. Sesame oil can thus reduce the risk of heart disease.
8. Anti-Cancer
Sesame oil has been identified as anti-cancer to specific cancers.
•Colon Cancer
•Brain cancer
•Ovarian cancer
•Lung cancer
•Prostate Cancer
•Renal cancer
•Melanoma ( skin cancer )
•Leukemia
The anti-cancer effect of sesame oil is due to a lignin called sesamin. It possesses cytotoxic effect against cancer cells, effectively blocking their proliferation.
9. Anti- Diabetes
Sesame oil shows blood glucose lowering effects in people when sesame is the oil they take in diet. This is of great significance. [6]
10. Going swimming in the pool, use sesame oil
Many people note that their hair starts turning greenish when they have been swimming regularly in the pool. This effect can be negated by applying sesame oil before or after the swim. It leaches out the chlorine. Applying it on the outer rim of the ear canal also prevents outer ear infection ( swimmer’s ear ).
11. Sesame oil in diet
One can cook with sesame oil. It has got a nice, sweet taste and mild aroma. Sesame oil can added to salads and foods like chicken. One can also use toasted sesame oil, which has a richer flavor and great for additive in recipes.
Generally, cooking with sesame oil is generally avoided. If you have a recipe which calls for toasted sesame oil, and you want a substitute for sesame oil, then use walnut oil or perilla oil.
10 Fabulous benefits of sesame oil for the beauty of your skin and hair
Sesame seeds come from the Sesame plant, which is grown in the wild in Africa and some parts of India. One of the oldest oilseed crops known to man, sesame seeds have one of the highest yields of oil of all of the oilseeds; the seeds are used extensively in cooking all around the world. Sesame seed oil is full of ingredients that can provide many different health benefits and it can also be used in many ways, as a part of your beauty regime, having benefits for hair, skin and anti-ageing properties. Here are ten of the beauty uses of sesame seed oil:
1. Promotes hair growth
Using sesame seed oil as a hot oil treatment for your hair is known to help the growth of thick and shiny hair. Massage some warm sesame seed oil into your hair and scalp (make sure that the oil isn’t too hot) and the essential fatty acids, such as omega-3, omega-6 and omega-9 will nourish your hair and encourage new growth.
2. Rich in vitamin E, sesame oil has anti-ageing properties for the skin
Benefits of sesame oil are very numerous. Sesame seed oil is a great source of vitamin E, the antioxidant used in many commercially available anti-ageing creams.
The vitamin E helps to fight the effects of free radicals, which are the main cause of premature ageing of the skin.
3. Removes skin blemishes
Sesame seed oil has anti-inflammatory and antibacterial properties, so it can be used as a treatment for skin conditions. Apply sesame oil before you go to bed at night and wash it off in the morning, and it will not only nourish and moisturize your skin, but also help fight blemishes and keep your skin clear and fresh looking.
4. Improves the skins elasticity and texture
Another one of great benefits of sesame oil is that the oil is slightly thicker, than most other natural oils, which makes it ideal for all over body massaging and skin conditioning. Sesame seed oil is widely used in India, where it is said that a daily massage with the oil removes the toxins and pollution from the skin, provides a deep down moisturising effect and improves elasticity of the skin.
5. Speeds up the healing process and reduce the appearance of scars
The vitamin E content and the antibacterial properties of sesame seed oil make it a good treatment for cuts and wounds. It promotes faster healing and it also reduces the appearance of scars on the skin.
6. Cracked heels and dry hands treatment
Next one of the amazing benefits of sesame oil is that the oil can help soothe and heal cracked heels and dry hands. Apply the oil to your feet and hands, and massage it in before going to bed and then, leave the oil on, with a pair of socks and cosy gloves on to keep the oil off the bed sheets and the oil will penetrate deep into the skin to bring quick relief to dry and cracked skin, making it softer, smoother and younger.
7. It can be used as a hair darkener
Used with other carrier oils like argan oil, coconut oil, olive oil or sweet almond oil, sesame oil is well known for its hair darkening properties. It is particularly effective as a means of minimising the appearance of premature greying of hair in people with black or brunette hair.
8. Conditions hair and prevents split ends
Another one of wonderful benefits of sesame oil is that the oil makes a great natural hair conditioner. It will give you a deep down condition that will nourish and moisturise your hair, treat and prevent split ends and leave your hair soft and bouncy. For a quick boost of moisture, mix a tablespoon of your regular hair conditioner or hair mask with a tablespoon of sesame oil in a bowl, then apply this mix to the length of your hair after shampooing, avoiding the roots. Leave it on for 3-5 minutes, then rinse off with lukewarm water. You will see how much silkier, softer and shinier your hair will become.
9. It protects your skin from damage
Used daily, sesame seed oil will give you protection from the harmful effects of the rays of the sun and provide a barrier against the effects of pollution and harmful toxins in the air. Whilst not a substitute for sunscreen, sesame seed oil does provide you with great, day to day, protection from the UV rays of the sun.
10. Treats a dry and flaky scalp
The vitamins, nutrients and minerals in sesame seed oil, along with its anti-fungal properties, make an excellent treatment for a dry and flaky scalp. At the other end of your body, it can also be used as treatment for athletes’ foot.
There are also many health benefits of sesame oil that we didn’t include in this article, this is why people love cooking with sesame oil, but this is another topic…
It’s best to buy organic sesame oil, especially if you want to use it for beauty.
Do you know some other sesame oil uses and benefits?
Please feel free to share your tips, tricks and secrets in the comment section below.
Stay happy and beautiful!
we talked about how the memory on a Linux guest is used for the OS itself (the kernel, buffers, etc.), applications, and also for file cache. File caching is an important performance improvement, and read caching is a clear win in most cases, balanced against applications using the RAM directly. Write caching is trickier. The Linux kernel stages disk writes into cache, and over time asynchronously flushes them to disk. This has a nice effect of speeding disk I/O but it is risky. When data isn’t written to disk there is an increased chance of losing it.
There is also the chance that a lot of I/O will overwhelm the cache, too. Ever written a lot of data to disk all at once, and seen large pauses on the system while it tries to deal with all that data? Those pauses are a result of the cache deciding that there’s too much data to be written asynchronously (as a non-blocking background operation, letting the application process continue), and switches to writing synchronously (blocking and making the process wait until the I/O is committed to disk). Of course, a filesystem also has to preserve write order, so when it starts writing synchronously it first has to destage the cache. Hence the long pause.
The nice thing is that these are controllable options, and based on your workloads & data you can decide how you want to set them up. Let’s take a look:
$ sysctl -a | grep dirty
vm.dirty_background_ratio = 10
vm.dirty_background_bytes = 0
vm.dirty_ratio = 20
vm.dirty_bytes = 0
vm.dirty_writeback_centisecs = 500
vm.dirty_expire_centisecs = 3000
vm.dirty_background_ratio is the percentage of system memory that can be filled with “dirty” pages — memory pages that still need to be written to disk — before the pdflush/flush/kdmflush background processes kick in to write it to disk. My example is 10%, so if my virtual server has 32 GB of memory that’s 3.2 GB of data that can be sitting in RAM before something is done.
vm.dirty_ratio is the absolute maximum amount of system memory that can be filled with dirty pages before everything must get committed to disk. When the system gets to this point all new I/O blocks until dirty pages have been written to disk. This is often the source of long I/O pauses, but is a safeguard against too much data being cached unsafely in memory.
vm.dirty_background_bytes and vm.dirty_bytes are another way to specify these parameters. If you set the _bytes version the _ratio version will become 0, and vice-versa.
vm.dirty_expire_centisecs is how long something can be in cache before it needs to be written. In this case it’s 30 seconds. When the pdflush/flush/kdmflush processes kick in they will check to see how old a dirty page is, and if it’s older than this value it’ll be written asynchronously to disk. Since holding a dirty page in memory is unsafe this is also a safeguard against data loss.
vm.dirty_writeback_centisecs is how often the pdflush/flush/kdmflush processes wake up and check to see if work needs to be done.
You can also see statistics on the page cache in /proc/vmstat:
$ cat /proc/vmstat | egrep "dirty|writeback"
nr_dirty 878
nr_writeback 0
nr_writeback_temp 0
In my case I have 878 dirty pages waiting to be written to disk.
Approach 1: Decreasing the Cache
As with most things in the computer world, how you adjust these depends on what you’re trying to do. In many cases we have fast disk subsystems with their own big, battery-backed NVRAM caches, so keeping things in the OS page cache is risky. Let’s try to send I/O to the array in a more timely fashion and reduce the chance our local OS will, to borrow a phrase from the service industry, be “in the weeds.” To do this we lower vm.dirty_background_ratio and vm.dirty_ratio by adding new numbers to /etc/sysctl.conf and reloading with “sysctl –p”:
vm.dirty_background_ratio = 5
vm.dirty_ratio = 10
This is a typical approach on virtual machines, as well as Linux-based hypervisors. I wouldn’t suggest setting these parameters to zero, as some background I/O is nice to decouple application performance from short periods of higher latency on your disk array & SAN (“spikes”).
Approach 2: Increasing the Cache
There are scenarios where raising the cache dramatically has positive effects on performance. These situations are where the data contained on a Linux guest isn’t critical and can be lost, and usually where an application is writing to the same files repeatedly or in repeatable bursts. In theory, by allowing more dirty pages to exist in memory you’ll rewrite the same blocks over and over in cache, and just need to do one write every so often to the actual disk. To do this we raise the parameters:
vm.dirty_background_ratio = 50
vm.dirty_ratio = 80
Sometimes folks also increase the vm.dirty_expire_centisecs parameter to allow more time in cache. Beyond the increased risk of data loss, you also run the risk of long I/O pauses if that cache gets full and needs to destage, because on large VMs there will be a lot of data in cache.
Approach 3: Both Ways
There are also scenarios where a system has to deal with infrequent, bursty traffic to slow disk (batch jobs at the top of the hour, midnight, writing to an SD card on a Raspberry Pi, etc.). In that case an approach might be to allow all that write I/O to be deposited in the cache so that the background flush operations can deal with it asynchronously over time:
vm.dirty_background_ratio = 5
vm.dirty_ratio = 80
Here the background processes will start writing right away when it hits that 5% ceiling but the system won’t force synchronous I/O until it gets to 80% full. From there you just size your system RAM and vm.dirty_ratio to be able to consume all the written data. Again, there are tradeoffs with data consistency on disk, which translates into risk to data. Buy a UPS and make sure you can destage cache before the UPS runs out of power. 🙂
No matter the route you choose you should always be gathering hard data to support your changes and help you determine if you are improving things or making them worse. In this case you can get data from many different places, including the application itself, /proc/vmstat, /proc/meminfo, iostat, vmstat, and many of the things in /proc/sys/vm
High i/o wait tuning for File Servers
Under “Server”?“General” tab, change “Priority” configuration to “-19”. Process priority can be set from -19 to 20, -19 is the highest, 20 is the lowest.
Change to ‘deadline’ I/O scheduler
From command line (change the device ‘sda’ to appropriate device):
echo “deadline” > /sys/block/sda/queue/scheduler
kernel parameter, change /boot/grub/menu.lst, add kernel parameter
elevator=deadline
Change VM parameters
There are two variables which control the behaviour of VM flushing and allocation and affect network and disk performance
vm.dirty_background_ratio
vm.dirty_ratio
To set these values from command line
echo 20 > /proc/sys/vm/dirty_background_ratio
echo 60 > /proc/sys/vm/dirty_ratio
to make it permanent, edit /etc/sysctl.conf:
vm.dirty_background_ratio = 20
vm.dirty_ratio = 60
Increase readahead
To get current readahead value:
$ blockdev –getra /dev/sda
256
To increase it to a higher value like 16K:
$ blockdev –setra 16384 /dev/sda
Disable updating access time stamp for file system
Edit /etc/fstab, remove “atime” attribute if there is, add “noatime” attribute. The noatime change can significantly improve your server’s file i/o performance.
#sample /etc/fstab line before change
LABEL=/ / ext3 defaults 1 1
#sample /etc/fstab line after noatime change
LABEL=/ / ext3 defaults ,noatime 1 1
Kernel Network Tuning
Add the follwing to /etc/sysctl.conf
#increase local ports
net.ipv4.ip_local_port_range = 1024 65535
#reduce the number of time_wait connections
#these 3 lines can reduce your time_wait count by several hundred percent.
#however you should not use the following lines in a NATed configuration.
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
Then call sysctl to make them active
sysctl -p
we talked about how the memory on a Linux guest is used for the OS itself (the kernel, buffers, etc.), applications, and also for file cache. File caching is an important performance improvement, and read caching is a clear win in most cases, balanced against applications using the RAM directly. Write caching is trickier. The Linux kernel stages disk writes into cache, and over time asynchronously flushes them to disk. This has a nice effect of speeding disk I/O but it is risky. When data isn’t written to disk there is an increased chance of losing it.
There is also the chance that a lot of I/O will overwhelm the cache, too. Ever written a lot of data to disk all at once, and seen large pauses on the system while it tries to deal with all that data? Those pauses are a result of the cache deciding that there’s too much data to be written asynchronously (as a non-blocking background operation, letting the application process continue), and switches to writing synchronously (blocking and making the process wait until the I/O is committed to disk). Of course, a filesystem also has to preserve write order, so when it starts writing synchronously it first has to destage the cache. Hence the long pause.
The nice thing is that these are controllable options, and based on your workloads & data you can decide how you want to set them up. Let’s take a look:
$ sysctl -a | grep dirty
vm.dirty_background_ratio = 10
vm.dirty_background_bytes = 0
vm.dirty_ratio = 20
vm.dirty_bytes = 0
vm.dirty_writeback_centisecs = 500
vm.dirty_expire_centisecs = 3000
vm.dirty_background_ratio is the percentage of system memory that can be filled with “dirty” pages — memory pages that still need to be written to disk — before the pdflush/flush/kdmflush background processes kick in to write it to disk. My example is 10%, so if my virtual server has 32 GB of memory that’s 3.2 GB of data that can be sitting in RAM before something is done.
vm.dirty_ratio is the absolute maximum amount of system memory that can be filled with dirty pages before everything must get committed to disk. When the system gets to this point all new I/O blocks until dirty pages have been written to disk. This is often the source of long I/O pauses, but is a safeguard against too much data being cached unsafely in memory.
vm.dirty_background_bytes and vm.dirty_bytes are another way to specify these parameters. If you set the _bytes version the _ratio version will become 0, and vice-versa.
vm.dirty_expire_centisecs is how long something can be in cache before it needs to be written. In this case it’s 30 seconds. When the pdflush/flush/kdmflush processes kick in they will check to see how old a dirty page is, and if it’s older than this value it’ll be written asynchronously to disk. Since holding a dirty page in memory is unsafe this is also a safeguard against data loss.
vm.dirty_writeback_centisecs is how often the pdflush/flush/kdmflush processes wake up and check to see if work needs to be done.
You can also see statistics on the page cache in /proc/vmstat:
$ cat /proc/vmstat | egrep “dirty|writeback”
nr_dirty 878
nr_writeback 0
nr_writeback_temp 0
In my case I have 878 dirty pages waiting to be written to disk.
Approach 1: Decreasing the Cache
As with most things in the computer world, how you adjust these depends on what you’re trying to do. In many cases we have fast disk subsystems with their own big, battery-backed NVRAM caches, so keeping things in the OS page cache is risky. Let’s try to send I/O to the array in a more timely fashion and reduce the chance our local OS will, to borrow a phrase from the service industry, be “in the weeds.” To do this we lower vm.dirty_background_ratio and vm.dirty_ratio by adding new numbers to /etc/sysctl.conf and reloading with “sysctl –p”:
vm.dirty_background_ratio = 5
vm.dirty_ratio = 10
This is a typical approach on virtual machines, as well as Linux-based hypervisors. I wouldn’t suggest setting these parameters to zero, as some background I/O is nice to decouple application performance from short periods of higher latency on your disk array & SAN (“spikes”).
Approach 2: Increasing the Cache
There are scenarios where raising the cache dramatically has positive effects on performance. These situations are where the data contained on a Linux guest isn’t critical and can be lost, and usually where an application is writing to the same files repeatedly or in repeatable bursts. In theory, by allowing more dirty pages to exist in memory you’ll rewrite the same blocks over and over in cache, and just need to do one write every so often to the actual disk. To do this we raise the parameters:
vm.dirty_background_ratio = 50
vm.dirty_ratio = 80
Sometimes folks also increase the vm.dirty_expire_centisecs parameter to allow more time in cache. Beyond the increased risk of data loss, you also run the risk of long I/O pauses if that cache gets full and needs to destage, because on large VMs there will be a lot of data in cache.
Approach 3: Both Ways
There are also scenarios where a system has to deal with infrequent, bursty traffic to slow disk (batch jobs at the top of the hour, midnight, writing to an SD card on a Raspberry Pi, etc.). In that case an approach might be to allow all that write I/O to be deposited in the cache so that the background flush operations can deal with it asynchronously over time:
vm.dirty_background_ratio = 5
vm.dirty_ratio = 80
Here the background processes will start writing right away when it hits that 5% ceiling but the system won’t force synchronous I/O until it gets to 80% full. From there you just size your system RAM and vm.dirty_ratio to be able to consume all the written data. Again, there are tradeoffs with data consistency on disk, which translates into risk to data. Buy a UPS and make sure you can destage cache before the UPS runs out of power. 🙂
No matter the route you choose you should always be gathering hard data to support your changes and help you determine if you are improving things or making them worse. In this case you can get data from many different places, including the application itself, /proc/vmstat, /proc/meminfo, iostat, vmstat, and many of the things in /proc/sys/vm. Good luck!
Releasing cached memory in Linux
Under normal circumstances, modern Linux systems try to cache into memory disk data that is accessed often. Sometimes, we have that much memory in the system that our kernel keeps filling up the memory by caching every piece of data we access.
Other times, because of the swappiness factor, active data finds its way into the swap instead of the main memory. I have seen this behavior in a few systems hosting databases, specially running mysql, and it is a serious performance hazard.
In order to fix systems like this, we need to fix the swappiness, drop the caches and, swapoff and swapon the system swap.
Decreasing swappiness
Thus, the first thing to do is to decrease the overall swappiness. This tells the system how often to store data into the swap instead of the main memory.
By default, most common Linux systems have this value set to 60. However, this value can be modified easily, not only during boot time, but also in hot.
The higher the value of the swappiness, the more often the system will try to store data in the swap. On the other hand, the lower the value of the swappiness, the more often the system will try to store data into main memory.
The swappiness value falls into the range of 0 (try not to swap at all) to 100 (try to swap as much as possible).
To modify the swappiness in hot, simply echo the value you desire to /proc/sys/vm/swappiness. For example:
1
# echo “10” > /proc/sys/vm/swappiness
To have this done at boot time, modify /etc/sysctl.conf and set a value for the entry (create it if it does not exist yet) vm.swappiness. For example:
Code:
1
# Set swappiness
2
vm.swappiness=10
Dropping the caches
Once this is done, the next thing we can do is to clean up the caches of the memory. Unfortunately, this feature is only present since kernels 2.6.16. Most people should not have problem with this, but a few may run out of luck. In any case, let me remark that this operation is safe under most circumstances.
Dropping the caches is as simple as echoing a value between 1 (one) and 3 (three) to /proc/sys/vm/drop_caches. Once this operation is done, you do not need to echo a 0 (zero) back to /proc/sys/vm/drop_caches, the system will start caching again from scratch automatically. This means that you cannot actually tell the system to stop caching using this method.
The values that can be echoed do the following:
1 : Drops pagecaches
2 : Drops dentries and inodes
3 : Drops pagecaches, dentries and inodes
Example:
1
# echo “1” > /proc/sys/vm/drop_caches
Swapping off and on
Now comes the slow part of swapping off. You should normally know where your system is swapping, but just in case you are new to this, run the following command:
1
# grep swap /etc/fstab
Running this command will produce output similar to the following:
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
Now that you have identified your partition, verify that its contents will fit into main memory by running the following command:
1
# free -m
This will produce output similar to the following:
total used free shared buffers cached
Mem: 8112 7823 288 0 12 6016
-/+ buffers/cache: 1794 6317
Swap: 1983 0 1983
Have a look at the value located in the swap row and the used column. If this value is higher than the value located in the mem row and the free column, it means that your swapped data won’t fit into your free main memory and, thus, the system will not allow you to.
Once you know that your swapped data fits into your free main memory, you can swapoff and swapon back your swap partition. Be very careful as this process can be very slow and cannot be normally stopped once it is running. You have been warned.
In my case, I would run something like the following:
1
# swapoff /dev/VolGroup00/LogVol01 && swapon /dev/VolGroup00/LogVol01
You may want to use something like nohup if you are connected to a remote machine and at the risk of losing the session. You can alternatively run the command into a screen as well.
How To Configure Swappiness
The operating system kernel can adjust how often it relies on swap through a configuration parameter known as swappiness.
To find the current swappiness settings, type:
cat /proc/sys/vm/swappiness
60
Swapiness can be a value from 0 to 100. Swappiness near 100 means that the operating system will swap often and usually, too soon. Although swap provides extra resources, RAM is much faster than swap space. Any time something is moved from RAM to swap, it slows down.
A swappiness value of 0 means that the operating will only rely on swap when it absolutely needs to. We can adjust the swappiness with the sysctl command:
sysctl vm.swappiness=10
vm.swappiness=10
If we check the system swappiness again, we can confirm that the setting was applied:
cat /proc/sys/vm/swappiness
10
To make your VPS automatically apply this setting every time it boots up, you can add the setting to the /etc/sysctl.conf file:
sudo nano /etc/sysctl.conf
# Search for the vm.swappiness setting. Uncomment and change it as necessary.
vm.swappiness=10
Try to add vm.swappiness = 0 to /etc/sysctl.conf and run sysctl -p then, this will instruct kernel to release memory occupied by the file cache more aggressively if a user application requires a memory region from kernel.
The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.
swappiness can have a value of between 0 and 100
swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible
swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache
The default setting in Linux is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical desktop installation. A value of swappiness=10 is recommended, but feel free to experiment.
To check the swappiness value use command: cat /proc/sys/vm/swappiness
To make a change permanent, edit the configuration file with your favorite editor:
vi /etc/sysctl.conf
and add following parameter to the end of the file like so:
vm.swappiness=10
Save the file and reboot.
Still, there are users who insist that, for example, a system backup should never force OpenOffice out to disk. They don’t care how quickly a system maintenance application runs at 3:00 in the morning,
but they care a lot about how the system responds when they are at the keyboard.
Swappiness should to be turned off
This one is pretty straightforward if you know about the Linux virtual memory system. Swappiness levels tell the virtual memory subsystem how much it should try and swap to disk. The thing is, the system will try to swap out items in memory even when there is plenty of RAM available to the system. The OS default is usually 60, which is a little aggressive IMO. You can see what value your system is set to by running the following command:
cat /proc/sys/vm/swappiness
Since Couchbase is tuned to really operate in memory as much as possible. You can gain or at minimum not lose performance by just changing the swappiness value to 0. In non-tech talk, this tells the virtual memory subsystem of the OS to not swap items from RAM to disk unless it really really has to, which if you have sized your nodes correctly, swapping should not be needed. To set this, perform the following process use sudo or just become root if you ride in the wild west.
# Set the value for the running system
sudo echo 0 > /proc/sys/vm/swappiness
# Backup sysctl.conf
sudo cp -p /etc/sysctl.conf /etc/sysctl.conf.`date +%Y%m%d-%H:%M`
# Set the value in /etc/sysctl.conf so it stays after reboot.
sudo echo ” >> /etc/sysctl.conf
sudo echo ‘#Set swappiness to 0 to avoid swapping’ >> /etc/sysctl.conf
sudo echo ‘vm.swappiness = 0’ >> /etc/sysctl.conf
Make sure that you either have or modify your process that builds your OSs to do this. This is especially critical for public/private clouds where it is so easy to bring up new instances. You need to make this part of your build process for a Couchbase node.
Disable Transparent Huge Pages (THP)
Starting in Red Hat Enterprise Linux (RHEL) version 6, so this includes CentOS 6 and 7 too, a new default method of managing huge pages was implemented in the OS. It combines smaller memory pages into Huge Pages without the running processes knowing. The idea is to reduce the number of lookups on TLB required and therefor increase performance. It brings in abstraction for automatation and management of huge pages basically. Couchbase Engineering has determined that under some conditions, Couchbase Server can be negatively impacted by severe page allocation delays when THP is enabled. Couchbase therefore recommends that THP be disabled on all Couchbase Server nodes
To disable this on a running system temporarily, run the following commands:
# Disable THP on a running system
sudo echo never > /sys/kernel/mm/transparent_hugepage/enabled
sudo echo never > /sys/kernel/mm/transparent_hugepage/defrag
To disable this permanently, do the following:
# Backup rc.local
sudo cp -p /etc/rc.local /etc/rc.local.`date +%Y%m%d-%H:%M`
Then copy the following into the bottom of /etc/rc.local.
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
If you do not want to reboot just yet, just use the echo commands above and know that the next time you reboot this will also be in place. Some people like to reboot if there is some change to such a file. Some do not.
Note: There is a different way to do this that you will find elsewhere and edits /etc/grub.conf. My problem with it is that it would get blown out with each and every kernel update in the future. What I propose is easier to manage in the long run and easy to put into something like Puppet module or Chef recipe to append to the end of rc.local when you boot strap a node.
THP is a great feature for some things, but causes problems with applications like Couchbase. It is not alone in this. If you go search the Internet for transparent huge pages, there are multiple documented issues from other DB and application vendors about this. Until something has been found to work with this, it is just best to turn THP off.
server 1 cluster1.rmohan.com cluster1 192.168.1.10
server 2 cluster2.rmohan.com cluster2 192.168.1.11
Download weblogic form the oracle website
fmw_12.1.3.0.0_wls.jar
mkdir software
64 BIT
# wget –no-cookies –no-check-certificate –header “Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie” “http://download.oracle.com/otn-pub/java/jdk/8u40-b25/jdk-8u40-linux-x64.tar.gz”
# tar xzf jdk-8u40-linux-x64.tar.gz
32 BIT
# wget –no-cookies –no-check-certificate –header “Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie” “http://download.oracle.com/otn-pub/java/jdk/8u40-b25/jdk-8u40-linux-i586.tar.gz”
# tar xzf jdk-8u40-linux-i586.tar.gz
mkdir /usr/java/
cd /usr/java/jdk1.8.0_40/
[root@cluster1 java]# ln -s /usr/java/jdk1.8.0_40/bin/java /usr/bin/java
[root@cluster1 java]# alternatives –install /usr/java/jdk1.8.0_40/bin/java java /usr/java/jdk1.8.0_40/bin/java 2
alternatives –install /usr/java/jdk1.8.0_40/bin/java java /usr/java/jdk1.8.0_40/bin/java 2
alternatives –config java
[root@cluster1 java]# alternatives –config java
There is 1 program that provides ‘java’.
Selection Command
———————————————–
*+ 1 /usr/java/jdk1.8.0_40/bin/java
Enter to keep the current selection[+], or type selection number: 1
[root@cluster1 java]#
alternatives –install /usr/bin/jar jar /opt/jdk1.8.0_40/bin/jar 2
alternatives –install /usr/bin/javac javac /opt/jdk1.8.0_40/bin/javac 2
alternatives –set jar /opt/jdk1.8.0_40/bin/jar
alternatives –set javac /opt/jdk1.8.0_40/bin/javac
vi /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_40
PATH=$JAVA_HOME/bin:$PATH
export PATH=$PATH:$JAVA_HOME
export JRE_HOME=/usr/java/jdk1.8.0_40/jre
export PATH=$PATH:/usr/java/jdk1.8.0_40/bin:/usr/java/jdk1.8.0_40/jre/bin
yum -y groupinstall “X Window System” “Desktop” “Fonts” “General Purpose Desktop”
useradd was
passwd was
xhost +
su – was
java -jar fmw_12.1.3.0.0_wls.jar
mkdir /u01/
chown -R was:was /u01/

Lets start the weblogic console
/u01/Oracle/Middleware/Oracle_Home/mohan/domains/mohan_domain/bin
nohup ./startWebLogic.sh &
weblogic console
Lets start the nodemanager
/u01/Oracle/Middleware/Oracle_Home/mohan/domains/mohan_domain/bin
[was@cluster1 bin]$ ls -ltr
total 76
drwxr-x— 2 was was 4096 Mar 20 21:36 server_migration
drwxr-x— 2 was was 4096 Mar 20 21:36 nodemanager
drwxr-x— 2 was was 4096 Mar 20 21:36 service_migration
-rwxr-x— 1 was was 1267 Mar 20 21:44 setStartupEnv.sh
-rwxr-x— 1 was was 5905 Mar 20 21:44 startWebLogic.sh
-rwxr-x— 1 was was 2068 Mar 20 21:44 stopWebLogic.sh
-rwxr-x— 1 was was 2653 Mar 20 21:44 stopManagedWebLogic.sh
-rwxr-x— 1 was was 3054 Mar 20 21:44 startManagedWebLogic.sh
-rwxr-x— 1 was was 13913 Mar 20 21:44 setDomainEnv.sh
-rwxr-x— 1 was was 862 Mar 20 21:44 stopRSDaemon.sh
-rwxr-x— 1 was was 1094 Mar 20 21:44 stopNodeManager.sh
-rwxr-x— 1 was was 714 Mar 20 21:44 startRSDaemon.sh
-rwxr-x— 1 was was 1070 Mar 20 21:44 startNodeManager.sh
-rwxr-x— 1 was was 2294 Mar 20 21:44 startComponent.sh
-rwxr-x— 1 was was 1958 Mar 20 21:44 stopComponent.sh
[was@cluster1 bin]$
[was@cluster1 bin]$ nohup ./startNodeManager.sh &
[1] 2549
[was@cluster1 bin]$ pwd
/u01/Oracle/Middleware/Oracle_Home/wlserver/common/bin
[was@cluster1 bin]$ ls -ltr
total 92
-rwxr-x— 1 was was 473 Mar 1 2012 reconfig.sh
-rwxr-x— 1 was was 487 Mar 1 2012 config_builder.sh
-rwxr-x— 1 was was 209 Mar 1 2012 commEnv.sh
-rwxr-x— 1 was was 491 Apr 16 2012 prepareCustomProvider.sh
-rwxr-x— 1 was was 1748 Dec 19 2012 wlst.sh
-rwxr-x— 1 was was 471 Dec 19 2012 unpack.sh
-rwxr-x— 1 was was 467 Dec 19 2012 pack.sh
-rwxr-x— 1 was was 471 Dec 19 2012 config.sh
-rwxr-x— 1 was was 483 Feb 6 2013 clonedunpack.sh
-rwxr-x— 1 was was 736 Feb 7 2013 qs_config.sh
-rwxr-x— 1 was was 16286 May 21 2014 wlsifconfig.sh
-rwxr-x— 1 was was 33538 Mar 20 21:22 wlscontrol.sh
[was@cluster1 bin]$
./pack.sh -domain=”/u01/Oracle/Middleware/Oracle_Home/mohan/domains/mohan_domain/” -template=”/u01/test/wlscltemp.jar” -template_name=”mohan_domain” log=”/u01/test/test.log” -log_priority=INFO
pack.sh -domain=”/export/home/wlsuser/domains/wlscldom”
-template=”/export/home/wlsuser/templates/wlscltemp.jar” -template_name=”WLS
CLUSTER DOMAIN” -log=/export/home/wlsuser/logs/wlscldompack.log -log_priority=INFO
commEnv.sh config.sh prepareCustomProvider.sh reconfig.sh wlscontrol.sh wlst.sh
[was@cluster1 bin]$ ./pack.sh -domain=”/u01/Oracle/Middleware/Oracle_Home/mohan/domains/mohan_domain/” -template=”/u01/test/wlscltemp.jar” -template_name=”mohan_domain” log=”/u01/test/test.log” -log_priority=INFO
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
Error: Argument log is not supported
[was@cluster1 bin]$ ./pack.sh -domain=”/u01/Oracle/Middleware/Oracle_Home/mohan/domains/mohan_domain/” -template=”/u01/test/wlscltemp.jar” -template_name=”mohan_domain”
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
<< read domain from “/u01/Oracle/Middleware/Oracle_Home/mohan/domains/mohan_domain”
>> succeed: read domain from “/u01/Oracle/Middleware/Oracle_Home/mohan/domains/mohan_domain”
<< write template to “/u01/test/wlscltemp.jar”
…………………………………………………………….. ……………….
>> succeed: write template to “/u01/test/wlscltemp.jar”
<< close template
>> succeed: close template
<< close template
>> succeed: close template
Trasfter the domain to other server
[was@cluster1 bin]$ scp /u01/test/wlscltemp.jar was@cluster2:/u01/test/
The authenticity of host ‘cluster2 (192.168.1.11)’ can’t be established.
RSA key fingerprint is 6f:79:4d:72:7d:c8:fb:02:f8:e7:4c:4c:c0:9a:f7:75.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘cluster2,192.168.1.11′ (RSA) to the list of known hosts.
was@cluster2’s password:
wlscltemp.jar 100% 54KB 53.5KB/s 00:00
[was@cluster1 bin]$
[was@cluster2 bin]$ ./unpack.sh -template=/u01/test/wlscltemp.jar -domain=domains/mohan_domain -log=unpack.log -log_priority=INFO
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
<< read template from “/u01/test/wlscltemp.jar”
tail -f unpack.log
2015-03-20 23:40:41,628 INFO [30] com.oracle.cie.domain.script.ScriptExecutor – succeed: read template from “/u01/test/wlscltemp.jar”
2015-03-20 23:40:41,633 INFO [30] com.oracle.cie.domain.script.ScriptExecutor – set config option DomainName to “mohan_domain”
2015-03-20 23:40:41,634 INFO [30] com.oracle.cie.domain.script.ScriptExecutor – succeed: set config option DomainName to “mohan_domain”
2015-03-20 23:40:41,635 INFO [30] com.oracle.cie.domain.script.ScriptExecutor – write Domain to “/u01/Oracle/Middleware/Oracle_Home/wlserver/common/bin/domains/mohan_domain”
[was@cluster2 bin]$ ls -ltr /u01/Oracle/Middleware/Oracle_Home/mohan/domains/mohan_domain/
ls: cannot access /u01/Oracle/Middleware/Oracle_Home/mohan/domains/mohan_domain/: No such file or directory
[was@cluster2 bin]$ ./unpack.sh -template=/u01/test/wlscltemp.jar -domain=domains/mohan_domain -log=unpack.log -log_priority=INFO
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
<< read template from “/u01/test/wlscltemp.jar”
>> succeed: read template from “/u01/test/wlscltemp.jar”
<< set config option DomainName to “mohan_domain”
>> succeed: set config option DomainName to “mohan_domain”
<< write Domain to “/u01/Oracle/Middleware/Oracle_Home/wlserver/common/bin/domains/mohan_domain”
………………………………………………………………………………
>> succeed: write Domain to “/u01/Oracle/Middleware/Oracle_Home/wlserver/common/bin/domains/mohan_domain”
<< close template
>> succeed: close template
[was@cluster2 bin]$
Lets start the nodemanger on the Node2 cluster2
/u01/Oracle/Middleware/Oracle_Home/wlserver/common/bin/domains/mohan_domain/bin
[was@cluster2 bin]$ nohup ./startNodeManager.sh &
[1] 24616
[was@cluster2 bin]$ nohup: ignoring input and appending output to `nohup.out’

Nginx
Nginx is a fast and lightweight alternative to the sometimes overbearing Apache 2. However, Nginx just like any kind of server or software must be tuned to help attain optimal performance.
The Architecture of Open Source Applications (Volume 2): nginx
nginx (pronounced “engine x”) is a free open source web server written by Igor Sysoev, a Russian software engineer. Since its public launch in 2004, nginx has focused on high performance, high concurrency and low memory usage. Additional features on top of the web server functionality, like load balancing, caching, access and bandwidth control, and the ability to integrate efficiently with a variety of applications, have helped to make nginx a good choice for modern website architectures. Currently nginx is the second most popular open source web server on the Internet.
The Architecture of Open Source Applications (Volume 2): nginx
14.1. Why Is High Concurrency Important?
These days the Internet is so widespread and ubiquitous it’s hard to imagine it wasn’t exactly there, as we know it, a decade ago. It has greatly evolved, from simple HTML producing clickable text, based on NCSA and then on Apache web servers, to an always-on communication medium used by more than 2 billion users worldwide. With the proliferation of permanently connected PCs, mobile devices and recently tablets, the Internet landscape is rapidly changing and entire economies have become digitally wired. Online services have become much more elaborate with a clear bias towards instantly available live information and entertainment. Security aspects of running online business have also significantly changed. Accordingly, websites are now much more complex than before, and generally require a lot more engineering efforts to be robust and scalable.
One of the biggest challenges for a website architect has always been concurrency. Since the beginning of web services, the level of concurrency has been continuously growing. It’s not uncommon for a popular website to serve hundreds of thousands and even millions of simultaneous users. A decade ago, the major cause of concurrency was slow clients—users with ADSL or dial-up connections. Nowadays, concurrency is caused by a combination of mobile clients and newer application architectures which are typically based on maintaining a persistent connection that allows the client to be updated with news, tweets, friend feeds, and so on. Another important factor contributing to increased concurrency is the changed behavior of modern browsers, which open four to six simultaneous connections to a website to improve page load speed.
To illustrate the problem with slow clients, imagine a simple Apache-based web server which produces a relatively short 100 KB response—a web page with text or an image. It can be merely a fraction of a second to generate or retrieve this page, but it takes 10 seconds to transmit it to a client with a bandwidth of 80 kbps (10 KB/s). Essentially, the web server would relatively quickly pull 100 KB of content, and then it would be busy for 10 seconds slowly sending this content to the client before freeing its connection. Now imagine that you have 1,000 simultaneously connected clients who have requested similar content. If only 1 MB of additional memory is allocated per client, it would result in 1000 MB (about 1 GB) of extra memory devoted to serving just 1000 clients 100 KB of content. In reality, a typical web server based on Apache commonly allocates more than 1 MB of additional memory per connection, and regrettably tens of kbps is still often the effective speed of mobile communications. Although the situation with sending content to a slow client might be, to some extent, improved by increasing the size of operating system kernel socket buffers, it’s not a general solution to the problem and can have undesirable side effects.
With persistent connections the problem of handling concurrency is even more pronounced, because to avoid latency associated with establishing new HTTP connections, clients would stay connected, and for each connected client there’s a certain amount of memory allocated by the web server.
Consequently, to handle the increased workloads associated with growing audiences and hence higher levels of concurrency—and to be able to continuously do so—a website should be based on a number of very efficient building blocks. While the other parts of the equation such as hardware (CPU, memory, disks), network capacity, application and data storage architectures are obviously important, it is in the web server software that client connections are accepted and processed. Thus, the web server should be able to scale nonlinearly with the growing number of simultaneous connections and requests per second.
Isn’t Apache Suitable?
Apache, the web server software that still largely dominates the Internet today, has its roots in the beginning of the 1990s. Originally, its architecture matched the then-existing operating systems and hardware, but also the state of the Internet, where a website was typically a standalone physical server running a single instance of Apache. By the beginning of the 2000s it was obvious that the standalone web server model could not be easily replicated to satisfy the needs of growing web services. Although Apache provided a solid foundation for future development, it was architected to spawn a copy of itself for each new connection, which was not suitable for nonlinear scalability of a website. Eventually Apache became a general purpose web server focusing on having many different features, a variety of third-party extensions, and universal applicability to practically any kind of web application development. However, nothing comes without a price and the downside to having such a rich and universal combination of tools in a single piece of software is less scalability because of increased CPU and memory usage per connection.
Thus, when server hardware, operating systems and network resources ceased to be major constraints for website growth, web developers worldwide started to look around for a more efficient means of running web servers. Around ten years ago, Daniel Kegel, a prominent software engineer, proclaimed that “it’s time for web servers to handle ten thousand clients simultaneously” and predicted what we now call Internet cloud services. Kegel’s C10K manifest spurred a number of attempts to solve the problem of web server optimization to handle a large number of clients at the same time, and nginx turned out to be one of the most successful ones.
Aimed at solving the C10K problem of 10,000 simultaneous connections, nginx was written with a different architecture in mind—one which is much more suitable for nonlinear scalability in both the number of simultaneous connections and requests per second. nginx is event-based, so it does not follow Apache’s style of spawning new processes or threads for each web page request. The end result is that even as load increases, memory and CPU usage remain manageable. nginx can now deliver tens of thousands of concurrent connections on a server with typical hardware.
When the first version of nginx was released, it was meant to be deployed alongside Apache such that static content like HTML, CSS, JavaScript and images were handled by nginx to offload concurrency and latency processing from Apache-based application servers. Over the course of its development, nginx has added integration with applications through the use of FastCGI, uswgi or SCGI protocols, and with distributed memory object caching systems like memcached. Other useful functionality like reverse proxy with load balancing and caching was added as well. These additional features have shaped nginx into an efficient combination of tools to build a scalable web infrastructure upon.
In February 2012, the Apache 2.4.x branch was released to the public. Although this latest release of Apache has added new multi-processing core modules and new proxy modules aimed at enhancing scalability and performance, it’s too soon to tell if its performance, concurrency and resource utilization are now on par with, or better than, pure event-driven web servers. It would be very nice to see Apache application servers scale better with the new version, though, as it could potentially alleviate bottlenecks on the backend side which still often remain unsolved in typical nginx-plus-Apache web configurations.
Are There More Advantages to Using nginx?
Handling high concurrency with high performance and efficiency has always been the key benefit of deploying nginx. However, there are now even more interesting benefits.
In the last few years, web architects have embraced the idea of decoupling and separating their application infrastructure from the web server. However, what would previously exist in the form of a LAMP (Linux, Apache, MySQL, PHP, Python or Perl)-based website, might now become not merely a LEMP-based one (`E’ standing for `Engine x’), but more and more often an exercise in pushing the web server to the edge of the infrastructure and integrating the same or a revamped set of applications and database tools around it in a different way.
nginx is very well suited for this, as it provides the key features necessary to conveniently offload concurrency, latency processing, SSL (secure sockets layer), static content, compression and caching, connections and requests throttling, and even HTTP media streaming from the application layer to a much more efficient edge web server layer. It also allows integrating directly with memcached/Redis or other “NoSQL” solutions, to boost performance when serving a large number of concurrent users.
With recent flavors of development kits and programming languages gaining wide use, more and more companies are changing their application development and deployment habits. nginx has become one of the most important components of these changing paradigms, and it has already helped many companies start and develop their web services quickly and within their budgets.
The first lines of nginx were written in 2002. In 2004 it was released to the public under the two-clause BSD license. The number of nginx users has been growing ever since, contributing ideas, and submitting bug reports, suggestions and observations that have been immensely helpful and beneficial for the entire community.
The nginx codebase is original and was written entirely from scratch in the C programming language. nginx has been ported to many architectures and operating systems, including Linux, FreeBSD, Solaris, Mac OS X, AIX and Microsoft Windows. nginx has its own libraries and with its standard modules does not use much beyond the system’s C library, except for zlib, PCRE and OpenSSL which can be optionally excluded from a build if not needed or because of potential license conflicts.
A few words about the Windows version of nginx. While nginx works in a Windows environment, the Windows version of nginx is more like a proof-of-concept rather than a fully functional port. There are certain limitations of the nginx and Windows kernel architectures that do not interact well at this time. The known issues of the nginx version for Windows include a much lower number of concurrent connections, decreased performance, no caching and no bandwidth policing. Future versions of nginx for Windows will match the mainstream functionality more closely.
14.2. Overview of nginx Architecture
Traditional process- or thread-based models of handling concurrent connections involve handling each connection with a separate process or thread, and blocking on network or input/output operations. Depending on the application, it can be very inefficient in terms of memory and CPU consumption. Spawning a separate process or thread requires preparation of a new runtime environment, including allocation of heap and stack memory, and the creation of a new execution context. Additional CPU time is also spent creating these items, which can eventually lead to poor performance due to thread thrashing on excessive context switching. All of these complications manifest themselves in older web server architectures like Apache’s. This is a tradeoff between offering a rich set of generally applicable features and optimized usage of server resources.
From the very beginning, nginx was meant to be a specialized tool to achieve more performance, density and economical use of server resources while enabling dynamic growth of a website, so it has followed a different model. It was actually inspired by the ongoing development of advanced event-based mechanisms in a variety of operating systems. What resulted is a modular, event-driven, asynchronous, single-threaded, non-blocking architecture which became the foundation of nginx code.
nginx uses multiplexing and event notifications heavily, and dedicates specific tasks to separate processes. Connections are processed in a highly efficient run-loop in a limited number of single-threaded processes called worker s. Within each worker nginx can handle many thousands of concurrent connections and requests per second.
Code Structure
The nginx worker code includes the core and the functional modules. The core of nginx is responsible for maintaining a tight run-loop and executing appropriate sections of modules’ code on each stage of request processing. Modules constitute most of the presentation and application layer functionality. Modules read from and write to the network and storage, transform content, do outbound filtering, apply server-side include actions and pass the requests to the upstream servers when proxying is activated.
nginx’s modular architecture generally allows developers to extend the set of web server features without modifying the nginx core. nginx modules come in slightly different incarnations, namely core modules, event modules, phase handlers, protocols, variable handlers, filters, upstreams and load balancers. At this time, nginx doesn’t support dynamically loaded modules; i.e., modules are compiled along with the core at build stage. However, support for loadable modules and ABI is planned for the future major releases
While handling a variety of actions associated with accepting, processing and managing network connections and content retrieval, nginx uses event notification mechanisms and a number of disk I/O performance enhancements in Linux, Solaris and BSD-based operating systems, like kqueue , epoll , and event ports . The goal is to provide as many hints to the operating system as possible, in regards to obtaining timely asynchronous feedback for inbound and outbound traffic, disk operations, reading from or writing to sockets, timeouts and so on. The usage of different methods for multiplexing and advanced I/O operations is heavily optimized for every Unix-based operating system nginx runs on.

The Architecture of Open Source Applications (Volume 2): nginx
Workers Model
As previously mentioned, nginx doesn’t spawn a process or thread for every connection. Instead, worker processes accept new requests from a shared “listen” socket and execute a highly efficient run-loop inside each worker to process thousands of connections per worker . There’s no specialized arbitration or distribution of connections to the worker s in nginx; this work is done by the OS kernel mechanisms. Upon startup, an initial set of listening sockets is created. worker s then continuously accept, read from and write to the sockets while processing HTTP requests and responses.
The run-loop is the most complicated part of the nginx worker code. It includes comprehensive inner calls and relies heavily on the idea of asynchronous task handling. Asynchronous operations are implemented through modularity, event notifications, extensive use of callback functions and fine-tuned timers. Overall, the key principle is to be as non-blocking as possible. The only situation where nginx can still block is when there’s not enough disk storage performance for a worker process.
Because nginx does not fork a process or thread per connection, memory usage is very conservative and extremely efficient in the vast majority of cases. nginx conserves CPU cycles as well because there’s no ongoing create-destroy pattern for processes or threads. What nginx does is check the state of the network and storage, initialize new connections, add them to the run-loop, and process asynchronously until completion, at which point the connection is deallocated and removed from the run-loop. Combined with the careful use of syscall s and an accurate implementation of supporting interfaces like pool and slab memory allocators, nginx typically achieves moderate-to-low CPU usage even under extreme workloads.
Because nginx spawns several worker s to handle connections, it scales well across multiple cores. Generally, a separate worker per core allows full utilization of multicore architectures, and prevents thread thrashing and lock-ups. There’s no resource starvation and the resource controlling mechanisms are isolated within single-threaded worker processes. This model also allows more scalability across physical storage devices, facilitates more disk utilization and avoids blocking on disk I/O. As a result, server resources are utilized more efficiently with the workload shared across several workers.
With some disk use and CPU load patterns, the number of nginx worker s should be adjusted. The rules are somewhat basic here, and system administrators should try a couple of configurations for their workloads. General recommendations might be the following: if the load pattern is CPU intensive—for instance, handling a lot of TCP/IP, doing SSL, or compression—the number of nginx worker s should match the number of CPU cores; if the load is mostly disk I/O bound—for instance, serving different sets of content from storage, or heavy proxying—the number of worker s might be one and a half to two times the number of cores. Some engineers choose the number of worker s based on the number of individual storage units instead, though efficiency of this approach depends on the type and configuration of disk storage.
One major problem that the developers of nginx will be solving in upcoming versions is how to avoid most of the blocking on disk I/O. At the moment, if there’s not enough storage performance to serve disk operations generated by a particular worker , that worker may still block on reading/writing from disk. A number of mechanisms and configuration file directives exist to mitigate such disk I/O blocking scenarios. Most notably, combinations of options like sendfile and AIO typically produce a lot of headroom for disk performance. An nginx installation should be planned based on the data set, the amount of memory available for nginx, and the underlying storage architecture.
Another problem with the existing worker model is related to limited support for embedded scripting. For one, with the standard nginx distribution, only embedding Perl scripts is supported. There is a simple explanation for that: the key problem is the possibility of an embedded script to block on any operation or exit unexpectedly. Both types of behavior would immediately lead to a situation where the worker is hung, affecting many thousands of connections at once. More work is planned to make embedded scripting with nginx simpler, more reliable and suitable for a broader range of applications.
nginx Process Roles
nginx runs several processes in memory; there is a single master process and several worker processes. There are also a couple of special purpose processes, specifically a cache loader and cache manager. All processes are single-threaded in version 1.x of nginx. All processes primarily use shared-memory mechanisms for inter-process communication. The master process is run as the root user. The cache loader, cache manager and worker s run as an unprivileged user.
The master process is responsible for the following tasks:
- reading and validating configuration
- creating, binding and closing sockets
- starting, terminating and maintaining the configured number of
worker processes
- reconfiguring without service interruption
- controlling non-stop binary upgrades (starting new binary and rolling back if necessary)
- re-opening log files
- compiling embedded Perl scripts
The worker processes accept, handle and process connections from clients, provide reverse proxying and filtering functionality and do almost everything else that nginx is capable of. In regards to monitoring the behavior of an nginx instance, a system administrator should keep an eye on worker s as they are the processes reflecting the actual day-to-day operations of a web server.
The cache loader process is responsible for checking the on-disk cache items and populating nginx’s in-memory database with cache metadata. Essentially, the cache loader prepares nginx instances to work with files already stored on disk in a specially allocated directory structure. It traverses the directories, checks cache content metadata, updates the relevant entries in shared memory and then exits when everything is clean and ready for use.
The cache manager is mostly responsible for cache expiration and invalidation. It stays in memory during normal nginx operation and it is restarted by the master process in the case of failure.
Brief Overview of nginx Caching
Caching in nginx is implemented in the form of hierarchical data storage on a filesystem. Cache keys are configurable, and different request-specific parameters can be used to control what gets into the cache. Cache keys and cache metadata are stored in the shared memory segments, which the cache loader, cache manager and worker s can access. Currently there is not any in-memory caching of files, other than optimizations implied by the operating system’s virtual filesystem mechanisms. Each cached response is placed in a different file on the filesystem. The hierarchy (levels and naming details) are controlled through nginx configuration directives. When a response is written to the cache directory structure, the path and the name of the file are derived from an MD5 hash of the proxy URL.
The process for placing content in the cache is as follows: When nginx reads the response from an upstream server, the content is first written to a temporary file outside of the cache directory structure. When nginx finishes processing the request it renames the temporary file and moves it to the cache directory. If the temporary files directory for proxying is on another file system, the file will be copied, thus it’s recommended to keep both temporary and cache directories on the same file system. It is also quite safe to delete files from the cache directory structure when they need to be explicitly purged. There are third-party extensions for nginx which make it possible to control cached content remotely, and more work is planned to integrate this functionality in the main distribution.
14.3. nginx Configuration
nginx’s configuration system was inspired by Igor Sysoev’s experiences with Apache. His main insight was that a scalable configuration system is essential for a web server. The main scaling problem was encountered when maintaining large complicated configurations with lots of virtual servers, directories, locations and datasets. In a relatively big web setup it can be a nightmare if not done properly both at the application level and by the system engineer himself.
As a result, nginx configuration was designed to simplify day-to-day operations and to provide an easy means for further expansion of web server configuration.
nginx configuration is kept in a number of plain text files which typically reside in /usr/local/etc/nginx or /etc/nginx . The main configuration file is usually called nginx.conf . To keep it uncluttered, parts of the configuration can be put in separate files which can be automatically included in the main one. However, it should be noted here that nginx does not currently support Apache-style distributed configurations (i.e., .htaccess files). All of the configuration relevant to nginx web server behavior should reside in a centralized set of configuration files.
The configuration files are initially read and verified by the master process. A compiled read-only form of the nginx configuration is available to the worker processes as they are forked from the master process. Configuration structures are automatically shared by the usual virtual memory management mechanisms.
nginx configuration has several different contexts for main , http , server , upstream , location (and also mail for mail proxy) blocks of directives. Contexts never overlap. For instance, there is no such thing as putting a location block in the main block of directives. Also, to avoid unnecessary ambiguity there isn’t anything like a “global web server” configuration. nginx configuration is meant to be clean and logical, allowing users to maintain complicated configuration files that comprise thousands of directives. In a private conversation, Sysoev said, “Locations, directories, and other blocks in the global server configuration are the features I never liked in Apache, so this is the reason why they were never implemented in nginx.”
Configuration syntax, formatting and definitions follow a so-called C-style convention. This particular approach to making configuration files is already being used by a variety of open source and commercial software applications. By design, C-style configuration is well-suited for nested descriptions, being logical and easy to create, read and maintain, and liked by many engineers. C-style configuration of nginx can also be easily automated.
While some of the nginx directives resemble certain parts of Apache configuration, setting up an nginx instance is quite a different experience. For instance, rewrite rules are supported by nginx, though it would require an administrator to manually adapt a legacy Apache rewrite configuration to match nginx style. The implementation of the rewrite engine differs too.
In general, nginx settings also provide support for several original mechanisms that can be very useful as part of a lean web server configuration. It makes sense to briefly mention variables and the try_files directive, which are somewhat unique to nginx. Variables in nginx were developed to provide an additional even-more-powerful mechanism to control run-time configuration of a web server. Variables are optimized for quick evaluation and are internally pre-compiled to indices. Evaluation is done on demand; i.e., the value of a variable is typically calculated only once and cached for the lifetime of a particular request. Variables can be used with different configuration directives, providing additional flexibility for describing conditional request processing behavior.
The try_files directive was initially meant to gradually replace conditional if configuration statements in a more proper way, and it was designed to quickly and efficiently try/match against different URI-to-content mappings. Overall, the try_files directive works well and can be extremely efficient and useful. It is recommended that the reader thoroughly check the try_files directive and adopt its use whenever applicable.
14.4. nginx Internals
As was mentioned before, the nginx codebase consists of a core and a number of modules. The core of nginx is responsible for providing the foundation of the web server, web and mail reverse proxy functionalities; it enables the use of underlying network protocols, builds the necessary run-time environment, and ensures seamless interaction between different modules. However, most of the protocol- and application-specific features are done by nginx modules, not the core.
Internally, nginx processes connections through a pipeline, or chain, of modules. In other words, for every operation there’s a module which is doing the relevant work; e.g., compression, modifying content, executing server-side includes, communicating to the upstream application servers through FastCGI or uwsgi protocols, or talking to memcached.
There are a couple of nginx modules that sit somewhere between the core and the real “functional” modules. These modules are http and mail . These two modules provide an additional level of abstraction between the core and lower-level components. In these modules, the handling of the sequence of events associated with a respective application layer protocol like HTTP, SMTP or IMAP is implemented. In combination with the nginx core, these upper-level modules are responsible for maintaining the right order of calls to the respective functional modules. While the HTTP protocol is currently implemented as part of the http module, there are plans to separate it into a functional module in the future, due to the need to support other protocols like SPDY (see “SPDY: An experimental protocol for a faster web“).
The functional modules can be divided into event modules, phase handlers, output filters, variable handlers, protocols, upstreams and load balancers. Most of these modules complement the HTTP functionality of nginx, though event modules and protocols are also used for mail . Event modules provide a particular OS-dependent event notification mechanism like kqueue or epoll . The event module that nginx uses depends on the operating system capabilities and build configuration. Protocol modules allow nginx to communicate through HTTPS, TLS/SSL, SMTP, POP3 and IMAP.
A typical HTTP request processing cycle looks like the following.
- Client sends HTTP request.
- nginx core chooses the appropriate phase handler based on the configured location matching the request.
- If configured to do so, a load balancer picks an upstream server for proxying.
- Phase handler does its job and passes each output buffer to the first filter.
- First filter passes the output to the second filter.
- Second filter passes the output to third (and so on).
- Final response is sent to the client.
nginx module invocation is extremely customizable. It is performed through a series of callbacks using pointers to the executable functions. However, the downside of this is that it may place a big burden on programmers who would like to write their own modules, because they must define exactly how and when the module should run. Both the nginx API and developers’ documentation are being improved and made more available to alleviate this.
Some examples of where a module can attach are:
- Before the configuration file is read and processed
- For each configuration directive for the location and the server where it appears
- When the main configuration is initialized
- When the server (i.e., host/port) is initialized
- When the server configuration is merged with the main configuration
- When the location configuration is initialized or merged with its parent server configuration
- When the master process starts or exits
- When a new worker process starts or exits
- When handling a request
- When filtering the response header and the body
- When picking, initiating and re-initiating a request to an upstream server
- When processing the response from an upstream server
- When finishing an interaction with an upstream server
Inside a worker , the sequence of actions leading to the run-loop where the response is generated looks like the following:
- Begin
ngx_worker_process_cycle() .
- Process events with OS specific mechanisms (such as
epoll or kqueue ).
- Accept events and dispatch the relevant actions.
- Process/proxy request header and body.
- Generate response content (header, body) and stream it to the client.
- Finalize request.
- Re-initialize timers and events.
The run-loop itself (steps 5 and 6) ensures incremental generation of a response and streaming it to the client.
A more detailed view of processing an HTTP request might look like this:
- Initialize request processing.
- Process header.
- Process body.
- Call the associated handler.
- Run through the processing phases.
Which brings us to the phases. When nginx handles an HTTP request, it passes it through a number of processing phases. At each phase there are handlers to call. In general, phase handlers process a request and produce the relevant output. Phase handlers are attached to the locations defined in the configuration file.
Phase handlers typically do four things: get the location configuration, generate an appropriate response, send the header, and send the body. A handler has one argument: a specific structure describing the request. A request structure has a lot of useful information about the client request, such as the request method, URI, and header.
When the HTTP request header is read, nginx does a lookup of the associated virtual server configuration. If the virtual server is found, the request goes through six phases:
- server rewrite phase
- location phase
- location rewrite phase (which can bring the request back to the previous phase)
- access control phase
- try_files phase
- log phase
In an attempt to generate the necessary content in response to the request, nginx passes the request to a suitable content handler. Depending on the exact location configuration, nginx may try so-called unconditional handlers first, like perl , proxy_pass , flv , mp4 , etc. If the request does not match any of the above content handlers, it is picked by one of the following handlers, in this exact order: random index , index , autoindex , gzip_static , static .
Indexing module details can be found in the nginx documentation, but these are the modules which handle requests with a trailing slash. If a specialized module like mp4 or autoindex isn’t appropriate, the content is considered to be just a file or directory on disk (that is, static) and is served by the static content handler. For a directory it would automatically rewrite the URI so that the trailing slash is always there (and then issue an HTTP redirect).
The content handlers’ content is then passed to the filters. Filters are also attached to locations, and there can be several filters configured for a location. Filters do the task of manipulating the output produced by a handler. The order of filter execution is determined at compile time. For the out-of-the-box filters it’s predefined, and for a third-party filter it can be configured at the build stage. In the existing nginx implementation, filters can only do outbound changes and there is currently no mechanism to write and attach filters to do input content transformation. Input filtering will appear in future versions of nginx.
Filters follow a particular design pattern. A filter gets called, starts working, and calls the next filter until the final filter in the chain is called. After that, nginx finalizes the response. Filters don’t have to wait for the previous filter to finish. The next filter in a chain can start its own work as soon as the input from the previous one is available (functionally much like the Unix pipeline). In turn, the output response being generated can be passed to the client before the entire response from the upstream server is received.
There are header filters and body filters; nginx feeds the header and the body of the response to the associated filters separately.
A header filter consists of three basic steps:
- Decide whether to operate on this response.
- Operate on the response.
- Call the next filter.
Body filters transform the generated content. Examples of body filters include:
- server-side includes
- XSLT filtering
- image filtering (for instance, resizing images on the fly)
- charset modification
gzip compression
- chunked encoding
After the filter chain, the response is passed to the writer. Along with the writer there are a couple of additional special purpose filters, namely the copy filter, and the postpone filter. The copy filter is responsible for filling memory buffers with the relevant response content which might be stored in a proxy temporary directory. The postpone filter is used for subrequests.
Subrequests are a very important mechanism for request/response processing. Subrequests are also one of the most powerful aspects of nginx. With subrequests nginx can return the results from a different URL than the one the client originally requested. Some web frameworks call this an internal redirect. However, nginx goes further—not only can filters perform multiple subrequests and combine the outputs into a single response, but subrequests can also be nested and hierarchical. A subrequest can perform its own sub-subrequest, and a sub-subrequest can initiate sub-sub-subrequests. Subrequests can map to files on the hard disk, other handlers, or upstream servers. Subrequests are most useful for inserting additional content based on data from the original response. For example, the SSI (server-side include) module uses a filter to parse the contents of the returned document, and then replaces include directives with the contents of specified URLs. Or, it can be an example of making a filter that treats the entire contents of a document as a URL to be retrieved, and then appends the new document to the URL itself.
Upstream and load balancers are also worth describing briefly. Upstreams are used to implement what can be identified as a content handler which is a reverse proxy (proxy_pass handler). Upstream modules mostly prepare the request to be sent to an upstream server (or “backend”) and receive the response from the upstream server. There are no calls to output filters here. What an upstream module does exactly is set callbacks to be invoked when the upstream server is ready to be written to and read from. Callbacks implementing the following functionality exist:
- Crafting a request buffer (or a chain of them) to be sent to the upstream server
- Re-initializing/resetting the connection to the upstream server (which happens right before creating the request again)
- Processing the first bits of an upstream response and saving pointers to the payload received from the upstream server
- Aborting requests (which happens when the client terminates prematurely)
- Finalizing the request when nginx finishes reading from the upstream server
- Trimming the response body (e.g. removing a trailer)
Load balancer modules attach to the proxy_pass handler to provide the ability to choose an upstream server when more than one upstream server is eligible. A load balancer registers an enabling configuration file directive, provides additional upstream initialization functions (to resolve upstream names in DNS, etc.), initializes the connection structures, decides where to route the requests, and updates stats information. Currently nginx supports two standard disciplines for load balancing to upstream servers: round-robin and ip-hash.
Upstream and load balancing handling mechanisms include algorithms to detect failed upstream servers and to re-route new requests to the remaining ones—though a lot of additional work is planned to enhance this functionality. In general, more work on load balancers is planned, and in the next versions of nginx the mechanisms for distributing the load across different upstream servers as well as health checks will be greatly improved.
There are also a couple of other interesting modules which provide an additional set of variables for use in the configuration file. While the variables in nginx are created and updated across different modules, there are two modules that are entirely dedicated to variables: geo and map . The geo module is used to facilitate tracking of clients based on their IP addresses. This module can create arbitrary variables that depend on the client’s IP address. The other module, map , allows for the creation of variables from other variables, essentially providing the ability to do flexible mappings of hostnames and other run-time variables. This kind of module may be called the variable handler.
Memory allocation mechanisms implemented inside a single nginx worker were, to some extent, inspired by Apache. A high-level description of nginx memory management would be the following: For each connection, the necessary memory buffers are dynamically allocated, linked, used for storing and manipulating the header and body of the request and the response, and then freed upon connection release. It is very important to note that nginx tries to avoid copying data in memory as much as possible and most of the data is passed along by pointer values, not by calling memcpy .
Going a bit deeper, when the response is generated by a module, the retrieved content is put in a memory buffer which is then added to a buffer chain link. Subsequent processing works with this buffer chain link as well. Buffer chains are quite complicated in nginx because there are several processing scenarios which differ depending on the module type. For instance, it can be quite tricky to manage the buffers precisely while implementing a body filter module. Such a module can only operate on one buffer (chain link) at a time and it must decide whether to overwrite the input buffer, replace the buffer with a newly allocated buffer, or insert a new buffer before or after the buffer in question. To complicate things, sometimes a module will receive several buffers so that it has an incomplete buffer chain that it must operate on. However, at this time nginx provides only a low-level API for manipulating buffer chains, so before doing any actual implementation a third-party module developer should become really fluent with this arcane part of nginx.
A note on the above approach is that there are memory buffers allocated for the entire life of a connection, thus for long-lived connections some extra memory is kept. At the same time, on an idle keepalive connection, nginx spends just 550 bytes of memory. A possible optimization for future releases of nginx would be to reuse and share memory buffers for long-lived connections.
The task of managing memory allocation is done by the nginx pool allocator. Shared memory areas are used to accept mutex, cache metadata, the SSL session cache and the information associated with bandwidth policing and management (limits). There is a slab allocator implemented in nginx to manage shared memory allocation. To allow simultaneous safe use of shared memory, a number of locking mechanisms are available (mutexes and semaphores). In order to organize complex data structures, nginx also provides a red-black tree implementation. Red-black trees are used to keep cache metadata in shared memory, track non-regex location definitions and for a couple of other tasks.
Unfortunately, all of the above was never described in a consistent and simple manner, making the job of developing third-party extensions for nginx quite complicated. Although some good documents on nginx internals exist—for instance, those produced by Evan Miller—such documents required a huge reverse engineering effort, and the implementation of nginx modules is still a black art for many.
Despite certain difficulties associated with third-party module development, the nginx user community recently saw a lot of useful third-party modules. There is, for instance, an embedded Lua interpreter module for nginx, additional modules for load balancing, full WebDAV support, advanced cache control and other interesting third-party work that the authors of this chapter encourage and will support in the future.
Starting, Stopping, and Reloading Configuration
To start nginx, run the executable file. Once nginx is started, it can be controlled by invoking the executable with the -s parameter. Use the following syntax:
nginx -s signal
Where signal may be one of the following:
• stop — fast shutdown
• quit — graceful shutdown
• reload — reloading the configuration file
• reopen — reopening the log files
For example, to stop nginx processes with waiting for the worker processes to finish serving current requests, the following command can be executed:
nginx -s quit
This command should be executed under the same user that started nginx.
Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted. To reload configuration, execute:
nginx -s reload
nginx.conf
user www www; ## Default: nobody
worker_processes 5; ## Default: 1
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 8192;
events {
worker_connections 4096; ## Default: 1024
}
http {
include conf/mime.types;
include /etc/nginx/proxy.conf;
include /etc/nginx/fastcgi.conf;
index index.html index.htm index.php;
default_type application/octet-stream;
log_format main ‘$remote_addr – $remote_user [$time_local] $status ‘
‘”$request” $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128; # this seems to be required for some vhosts
server { # php/fastcgi
listen 80;
server_name domain1.com www.domain1.com;
access_log logs/domain1.access.log main;
root html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:1025;
}
}
server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
expires 30d;
}
# pass requests for dynamic content to rails/turbogears/zope, et al
location / {
proxy_pass http://127.0.0.1:8080;
}
}
upstream big_server_com {
server 127.0.0.3:8000 weight=5;
server 127.0.0.3:8001 weight=5;
server 192.168.0.1:8000;
server 192.168.0.1:8001;
}
server { # simple load balancing
listen 80;
server_name big.server.com;
access_log logs/big.server.access.log main;
location / {
proxy_pass http://big_server_com;
}
}
}
Auxilary Files
proxy_conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
fastcgi_conf
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_index index.php;
fastcgi_param REDIRECT_STATUS 200;
mime_types
types {
text/html html htm shtml;
text/css css;
text/xml xml rss;
image/gif gif;
image/jpeg jpeg jpg;
application/x-javascript js;
text/plain txt;
text/x-component htc;
text/mathml mml;
image/png png;
image/x-icon ico;
image/x-jng jng;
image/vnd.wap.wbmp wbmp;
application/java-archive jar war ear;
application/mac-binhex40 hqx;
application/pdf pdf;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/zip zip;
application/octet-stream deb;
application/octet-stream bin exe dll;
application/octet-stream dmg;
application/octet-stream eot;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/mpeg mp3;
audio/x-realaudio ra;
video/mpeg mpeg mpg;
video/quicktime mov;
video/x-flv flv;
video/x-msvideo avi;
video/x-ms-wmv wmv;
video/x-ms-asf asx asf;
video/x-mng mng;
}
Nginx Server
For demonstration purposes, we’re going to set up two domains with our Nginx server. The domain names we’ll use in this guide are example.com and test.com.
Step One — Set Up New Document Root Directories
It is configured to serve documents out of a directory at /usr/share/nginx/html
We won’t use the default since it is easier to work with things in the /var/www directory
mkdir -p /var/www/example.com/html
mkdir -p /var/www/test.com/html
chmod -R 755 /var/www
Create Sample Pages for Each Site
Now that we have our directory structure set up, let’s create a default page for each of our sites so that we will have something to display.
Create an index.html file in your first domain:
nano /var/www/example.com/html/index.html
Inside the file, we’ll create a really basic file that indicates what site we are currently accessing. It will look like this:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com server block is working!</h1>
</body>
</html>
Save and close the file when you are finished.
Since the file for our second site is basically going to be the same, we can copy it over to our second document root like this:
cp /var/www/example.com/html/index.html /var/www/test.com/html/
Now, we can open the new file in our editor and modify it so that it refers to our second domain:
nano /var/www/test.com/html/index.html
<html>
<head>
<title>Welcome to Test.com!</title>
</head>
<body>
<h1>Success! The test.com server block is working!</h1>
</body>
</html>
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
Now, open the new file you created in your text editor with root privileges:
nano /etc/nginx/sites-available/example.com
Ignoring the commented lines, the file will look similar to this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
}
Only one of our server blocks can have the default_server specification. This specifies which block should server a request if the server_name requested does not match any of the available server blocks.
We are eventually going to disable the default server block configuration, so we can place the default_server option in either this server block or in the one for our other site. I’m going to leave the default_server option enabled in this server block, but you can choose whichever is best for your situation.
The next thing we’re going to have to adjust is the document root, specified by the root directive. Point it to the site’s document root that you created:
root /var/www/example.com/html;
Note: Each Nginx statement must end with a semi-colon (;), so check each of your lines if you are running into problems.
Next, we want to modify the server_name to match requests for our first domain. We can additionally add any aliases that we want to match. We will add a www.example.com alias to demonstrate:
server_name example.com www.example.com;
When you are finished, your file will look something like this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/example.com/html;
index index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
That is all we need for a basic configuration. Save and close the file to exit.
Create the Second Server Block File
Now that we have our initial server block configuration, we can use that as a basis for our second file. Copy it over to create a new file:
cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/test.com
Open the new file with root privileges in your editor:
nano /etc/nginx/sites-available/test.com
In this new file, we’re going to have to look at the listen directives again. If you left the default_server option enabled in the last file, you’ll have to remove it in this file. Furthermore, you’ll have to get rid of the ipv6only=on option, as it can only be specified once per address/port combination:
listen 80;
listen [::]:80;
Adjust the document root directive to point to your second domain’s document root:
root /var/www/test.com/html;
Adjust the server_name to match your second domain and any aliases:
server_name test.com www.test.com;
Your file should look something like this with these changes:
server {
listen 80;
listen [::]:80;
root /var/www/test.com/html;
index index.html index.htm;
server_name test.com www.test.com;
location / {
try_files $uri $uri/ =404;
}
}
When you are finished, save and close the file.
Step Four — Enable your Server Blocks and Restart Nginx
You now have your server blocks created, we need to enable them.
We can do this by creating symbolic links from these files to the sites-enabled directory, which Nginx reads from during startup.
We can create these links by typing:
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/
These files are now in the enabled directory. However, the default server block file we used as a template is also enabled currently and will conflict with our file that has the default_server parameter set.
We can disable the default server block file by simply removing the symbolic link. It will still be available for reference in the sites-available directory, but it won’t be read by Nginx on startup:
rm /etc/nginx/sites-enabled/default
We also need to adjust one setting really quickly in the default Nginx configuration file. Open it up by typing:
nano /etc/nginx/nginx.conf
We just need to uncomment one line. Find and remove the comment from this:
server_names_hash_bucket_size 64;
Now, we are ready to restart Nginx to enable your changes. You can do that by typing:
service nginx restart
Step Six — Test your Results
Now that you are all set up, you should test that your server blocks are functioning correctly. You can do that by visiting the domains in your web browser:
http://example.com
You should see a page that looks like this:
Nginx first server block
If you visit your second domain name, you should see a slightly different site:
http://test.com
Nginx Load Balancing
We need to add the load balancing configuration to the file.
First we need to include the upstream module which looks like this:
upstream backend {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}
We should then reference the module further on in the configuration:
server {
location / {
proxy_pass http://backend;
}
}
Directives
The previous section covered how to equally distribute load across several virtual servers. However, there are many reasons why this may not be the most efficient way to work with data. There are several directives that we can use to direct site visitors more effectively.
Weight
One way to begin to allocate users to servers with more precision is to allocate specific weight to certain machines. Nginx allows us to assign a number specifying the proportion of traffic that should be directed to each server.
A load balanced setup that included server weight could look like this:
upstream backend {
server backend1.example.com weight=1;
server backend2.example.com weight=2;
server backend3.example.com weight=4;
}
The default weight is 1. With a weight of 2, backend2.example will be sent twice as much traffic as backend1, and backend3, with a weight of 4, will deal with twice as much traffic as backend2 and four times as much as backend 1.
Hash
IP hash allows servers to respond to clients according to their IP address, sending visitors back to the same VPS each time they visit (unless that server is down). If a server is known to be inactive, it should be marked as down. All IPs that were supposed to routed to the down server are then directed to an alternate one.
The configuration below provides an example:
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
}
Max Fails
According to the default round robin settings, nginx will continue to send data to the virtual private servers, even if the servers are not responding. Max fails can automatically prevent this by rendering unresponsive servers inoperative for a set amount of time.
There are two factors associated with the max fails: max_fails and fall_timeout. Max fails refers to the maximum number of failed attempts to connect to a server should occur before it is considered inactive. Fall_timeout specifies the length of that the server is considered inoperative. Once the time expires, new attempts to reach the server will start up again. The default timeout value is 10 seconds.
A sample configuration might look like this:
upstream backend {
server backend1.example.com max_fails=3 fail_timeout=15s;
server backend2.example.com weight=2;
server backend3.example.com weight=4;
CentOS 6.5 Install Problem
CentOS 6.5 Install Problem
I have recently installed CentOS 6.5 on a couple of computers, once without any problems and once with a fatal hang during install. With both installs I chose to overwrite any existing partitions and use the whole hard-drive.The problem during the failed install happened as the new partitions were being created. An error window including ‘Unable to read group information from repositories’ had a Retry button that did nothing. Google was no help (other than to say it might be a DVD read problem) but with some persistence I found a straight-forward solution.
If you get this error, power off the computer then start the installation process again. This time choose to overwrite an existing installation and CentOS will install without this problem recurring.
CentOS compile and install httpd-2.4 and 2.4 versions of the 6.5 features introduced
httpd-2.4’s new features:
1) MPM is loaded at run-time support;
–enalbe All-mpm-shared –with-mpm = prefork = {| worker | Event}
2) Support Event mpm
3) asynchronous read and write
4) in each module and each directory separately use a different log levels
5) configure each request: <If>, <Else if>
6) Enhanced expression parser
7) millisecond keep alive the timeout
8) FQDN based virtual hosts NameVirtualHost directive is no longer needed;
9) supports user-defined variables
Additional modules :
mod_proxy_fcgi: support for ways to connect with fastcgi backend php
mode_ratelimit: speed limit, restrict user access to the transmission rate
mod_request: user requests to do more filtering
mod_remoteip: user access to IP addresses have more control
Modify some configuration mechanism :
No longer supports the use of order, allow, deny ip based access control definitions, to require
Dependencies :
Because httpd program relies on apr and apr-util so before installing httpd need to install apr and apr-util, apr yum source centos6.5 provided for httpd 1.3.9 version 1.4.0 version supports more than needed, so apr and apr-util also need to be manually installed
yum install openssl*
yum -y install arp apr-devel apr-util apr-util-devel pcre pcre-devel (Important one )
yum groupinstall ‘Development tools’
yum groupinstall ‘Server Platform Development’
wget https://archive.apache.org/dist/apr/apr-1.5.0.tar.gz
wget https://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gz
wget http://mirror.nus.edu.sg/apache//httpd/httpd-2.4.12.tar.gz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre2-10.10.tar.gz
tar zxvf apr-1.5.0.tar.gz
tar zxvf apr-util-1.5.4.tar.gz
tar zxvf httpd-2.4.12.tar.gz
tar -zxvf pcre2-10.10.tar.gz
cd /root/software/apr-1.5.0
./configure –prefix=/usr/local/apr
make && make install
[root@cluster1 apr-1.5.0]# ls /usr/local/apr/
bin build-1 include lib
[root@cluster1 apr-1.5.0]#
cd apr-util-1.5.4
./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr
make && make install
cd /root/software/pcre2-10.10
./configure –prefix=/usr/local/pcre
make && make install
[root@cluster1 httpd-2.4.12]# ls -ltr /usr/local/
total 52
drwxr-xr-x. 2 root root 4096 Sep 23 2011 src
drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib64
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23 2011 include
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 bin
drwxr-xr-x. 5 root root 4096 Feb 19 19:54 share
drwxr-xr-x. 6 root root 4096 Mar 18 18:43 apr
drwxr-xr-x. 5 root root 4096 Mar 18 18:45 apr-util
drwxr-xr-x. 6 root root 4096 Mar 18 18:55 pcre
cd /root/software/httpd-2.4.12
./configure –prefix=/usr/local/apache –sysconfdir=/etc/httpd –enable-so –enable-ssl –enable-cgi –enable-rewrite –with-pcre –with-zlib –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util –enable-mpms-shared=all –with-mpm=event –enable-modules=most –enable-file-cache –enable-cache –enable-disk-cache –enable-mem-cache –enable-deflate –enable-expires –enable-headers –enable-usertrack
./configure –enable-file-cache –enable-cache –enable-disk-cache –enable-mem-cache –enable-deflate –enable-expires –enable-headers –enable-usertrack –enable-ssl –enable-cgi –enable-vhost-alias –enable-rewrite –enable-so –with-apr=/usr/local/apr/
make && make install
elinks http://localhost
It works!
[root@test2 httpd]# vim httpd.conf
# Server-pool management (MPM specific)
Include /etc/httpd/extra/httpd-mpm.conf
[root@cluster1 extra]# ls
httpd-autoindex.conf httpd-default.conf httpd-languages.conf httpd-mpm.conf httpd-ssl.conf httpd-vhosts.conf
httpd-dav.conf httpd-info.conf httpd-manual.conf httpd-multilang-errordoc.conf httpd-userdir.conf proxy-html.conf
[root@cluster1 extra]# cat httpd-mpm.conf
#
# Server-Pool Management (MPM specific)
#
#
# PidFile: The file in which the server should record its process
# identification number when it starts.
#
# Note that this is the default PidFile for most MPMs.
#
<IfModule !mpm_netware_module>
PidFile “logs/httpd.pid”
</IfModule>
#
# Only one of the below sections will be relevant on your
# installed httpd. Use “apachectl -l” to find out the
# active mpm.
#
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxRequestWorkers: maximum number of server processes allowed to start
# MaxConnectionsPerChild: maximum number of connections a server process serves
# before terminating
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 250
MaxConnectionsPerChild 0
</IfModule>
# worker MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestWorkers: maximum number of worker threads
# MaxConnectionsPerChild: maximum number of connections a server process serves
# before terminating
<IfModule mpm_worker_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>
# event MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestWorkers: maximum number of worker threads
# MaxConnectionsPerChild: maximum number of connections a server process serves
# before terminating
<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>
# NetWare MPM
# ThreadStackSize: Stack size allocated for each worker thread
# StartThreads: Number of worker threads launched at server startup
# MinSpareThreads: Minimum number of idle threads, to handle request spikes
# MaxSpareThreads: Maximum number of idle threads
# MaxThreads: Maximum number of worker threads alive at the same time
# MaxConnectionsPerChild: Maximum number of connections a thread serves. It
# is recommended that the default value of 0 be set
# for this directive on NetWare. This will allow the
# thread to continue to service requests indefinitely.
<IfModule mpm_netware_module>
ThreadStackSize 65536
StartThreads 250
MinSpareThreads 25
MaxSpareThreads 250
MaxThreads 1000
MaxConnectionsPerChild 0
</IfModule>
# OS/2 MPM
# StartServers: Number of server processes to maintain
# MinSpareThreads: Minimum number of idle threads per process,
# to handle request spikes
# MaxSpareThreads: Maximum number of idle threads per process
# MaxConnectionsPerChild: Maximum number of connections per server process
<IfModule mpm_mpmt_os2_module>
StartServers 2
MinSpareThreads 5
MaxSpareThreads 10
MaxConnectionsPerChild 0
</IfModule>
# WinNT MPM
# ThreadsPerChild: constant number of worker threads in the server process
# MaxConnectionsPerChild: maximum number of connections a server process serves
<IfModule mpm_winnt_module>
ThreadsPerChild 150
MaxConnectionsPerChild 0
</IfModule>
# The maximum number of free Kbytes that every allocator is allowed
# to hold without calling free(). In threaded MPMs, every thread has its own
# allocator. When not set, or when set to zero, the threshold will be set to
# unlimited.
<IfModule !mpm_netware_module>
MaxMemFree 2048
</IfModule>
<IfModule mpm_netware_module>
MaxMemFree 100
</IfModule>
[root@cluster1 extra]#
vi httpd.conf
module is loaded
#LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
LoadModule mpm_event_module modules/mod_mpm_event.so
CHECK THE MODULES LOADED
[root@cluster1 httpd]# /usr/local/apache/bin/httpd -M
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
authn_file_module (shared)
authn_core_module (shared)
authz_host_module (shared)
authz_groupfile_module (shared)
authz_user_module (shared)
authz_core_module (shared)
access_compat_module (shared)
auth_basic_module (shared)
reqtimeout_module (shared)
filter_module (shared)
mime_module (shared)
log_config_module (shared)
env_module (shared)
headers_module (shared)
setenvif_module (shared)
version_module (shared)
mpm_event_module (shared)
unixd_module (shared)
status_module (shared)
autoindex_module (shared)
dir_module (shared)
alias_module (shared)
[root@cluster1 httpd]#
.htaccess or mod_rewrite encyclopedia
collected here are a variety of practical .htaccess snippets, you can think of the use of almost all here.
Disclaimer : Although these snippets directly copied to your .htaccess file, the vast majority of cases are easy to use, but there are very few cases where you need to modify some of the job. At your own risk.
IMPORTANT NOTE : Apache 2.4 is not compatible changes, especially in terms of access to configuration control. For more information, please refer to this updated document and article .
Table of Contents
Again and redirection
Forced www
Www mandatory general method
Forced non-www
Forced non-www common method
Forced HTTPS
By forcing HTTPS proxy
Add a slash at the end of compulsory
Remove trailing slashes
Redirect to a page
Directory alias
Script aliases
Redirect the entire site
Clean URL
Security
Deny all access
Deny all access (excluding section)
Shield reptile / malicious access
Hidden files and directories
Protect the backup files and source code files
Ban directory browsing
Prohibit pictures Daolian
Prohibit pictures hotlinking (specify name)
Password Protected Directories
Password-protected files
Visitor Referrer filtered through
Prevent nesting other pages
Performance
Archive
Set an expiration header
Close eTags logo
Other
Setting up a PHP variable
Custom Error Pages
Forced Download
Blocking the download
Run cross-domain font reference
Auto UTF-8 Encode
Switching PHP version
Prohibit IE Compatibility View
Supports WebP image format
Again and redirection
Note: you first need to install and enable server mod_rewrite module.
Forced www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
Www mandatory general method
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This method can be used at any site.
Forced non-www
WWW what is good, or non-www good, no conclusion, if you like without the www, you can use the following script:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Forced non-www common method
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
Forced HTTPS
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# Note: It’s also recommended to enable HTTP Strict Transport Security (HSTS)
# on your HTTPS website to help prevent man-in-the-middle attacks.
# See https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security
<IfModule mod_headers.c>
Header always set Strict-Transport-Security “max-age=31536000; includeSubDomains”
</IfModule>
By forcing HTTPS proxy
If you use a proxy, this method is useful for you.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Add a slash at the end of compulsory
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
Redirect to a page
Redirect 301 /oldpage.html http://www.example.com/newpage.html
Redirect 301 /oldpage2.html http://www.example.com/folder/
Source
Directory alias
RewriteEngine On
RewriteRule ^source-directory/(.*) target-directory/$1
Script aliases
FallbackResource /index.fcgi
This example has an index.fcgi file in some directory, and any requests within that directory that fail to
resolve a filename/directory will be sent to the index.fcgi script.
It’s good if you want baz.foo/some/cool/path to be handled by baz.foo/index.fcgi (which also supports requests to baz.foo)
while maintaining baz.foo/css/style.css and the like. Get access to the original path from the PATH_INFO environment variable,
as exposed to your scripting environment.
RewriteEngine On
RewriteRule ^$ index.fcgi/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]
This is a less efficient version of the FallbackResource directive (because using mod_rewrite is more complex than just handling the FallbackResource directive), but it’s also more flexible.
Redirect the entire site
Redirect 301 / http://newsite.com/
This way does it with links intact. That is www.oldsite.com/some/crazy/link.html will become
www.newsite.com/some/crazy/link.html. This is extremely helpful when you are just “moving” a site to a new domain. Source
Clean URL
This snippet lets you use “clean” URLs — those without a PHP extension, e.g.
example.com/users instead of example.com/users.php.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
Source
Security
Deny all access
## Apache 2.2
Deny from all
## Apache 2.4
# Require all denied
But wait, this will lock you out from your content as well! Thus introducing…
Deny all access (excluding section)
## Apache 2.2
Order deny,allow
Deny from all
Allow from xxx.xxx.xxx.xxx
## Apache 2.4
# Require all denied
# Require ip xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx is your IP. If you replace the last three digits with 0/12 for example, this will specify a range of IPs within the same network, thus saving you the trouble to list all allowed IPs separately.
Source
Now of course there’s a reversed version:
Shield reptile / malicious access
## Apache 2.2
Order deny,allow
Allow from all
Deny from xxx.xxx.xxx.xxx
Deny from xxx.xxx.xxx.xxy
## Apache 2.4
# Require all granted
# Require not ip xxx.xxx.xxx.xxx
# Require not ip xxx.xxx.xxx.xxy
Hidden files and directories
Hidden files and directories (those whose names start with a dot .) should most, if not all, of the time be secured. For example: .htaccess, .htpasswd, .git, .hg…
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule “(^|/)\.” – [F]
Alternatively, you can just raise a Not Found error, giving the attacker dude no clue:
RedirectMatch 404 /\..*$
Protect the backup files and source code files
These files may be left by some text/html editors (like Vi/Vim) and pose a great security danger if exposed to public.
<FilesMatch “(\.(bak|config|dist|fla|inc|ini|log|psd|sh|sql|swp)|~)$”>
## Apache 2.2
Order allow,deny
Deny from all
Satisfy All
## Apache 2.4
# Require all denied
</FilesMatch>
Source
Ban directory browsing
Options All -Indexes
Prohibit pictures Daolian
RewriteEngine on
# Remove the following line if you want to block blank referrer too
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(.+\.)?example.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|bmp)$ – [NC,F,L]
# If you want to display a “blocked” banner in place of the hotlinked image,
# replace the above rule with:
# RewriteRule \.(jpg|jpeg|png|gif|bmp) http://example.com/blocked.png [R,L]
Prohibit pictures hotlinking (specify name)
Sometimes you want to from some bad guys only.
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http(s)?://(.+\.)?badsite\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http(s)?://(.+\.)?badsite2\.com [NC,OR]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
# If you want to display a “blocked” banner in place of the hotlinked image,
# replace the above rule with:
# RewriteRule \.(jpg|jpeg|png|gif|bmp) http://example.com/blocked.png [R,L]
Password Protected Directories
First you need to create a .htpasswd file somewhere in the system:
htpasswd -c /home/fellowship/.htpasswd boromir
Then you can use it for authentication:
AuthType Basic
AuthName “One does not simply”
AuthUserFile /home/fellowship/.htpasswd
Require valid-user
AuthName “One still does not simply”
AuthType Basic
AuthUserFile /home/fellowship/.htpasswd
<Files “one-ring.o”>
Require valid-user
</Files>
<FilesMatch ^((one|two|three)-rings?\.o)$>
Require valid-user
</FilesMatch>
Visitor Referrer filtered through
This denies access for all users who are coming from (referred by) a specific domain.
Source
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} somedomain\.com [NC,OR]
RewriteCond %{HTTP_REFERER} anotherdomain\.com
RewriteRule .* – [F]
Prevent nesting other pages
This prevents the website to be framed (i.e. put into an iframe tag), when still allows framing for a specific URI.
SetEnvIf Request_URI “/starry-night” allow_framing=true
Header set X-Frame-Options SAMEORIGIN env=!allow_framing
Performance
Archive
<IfModule mod_deflate.c>
# ?? compression for mangled headers.
# http://developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding “gzip,deflate” env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
# Compress all output labeled with one of the following MIME-types
# (for Apache versions below 2.3.7, you don’t need to enable `mod_filter`
# and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines
# as `AddOutputFilterByType` is still in the core directives).
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/rss+xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
application/x-web-app-manifest+json \
application/xhtml+xml \
application/xml \
font/opentype \
image/svg+xml \
image/x-icon \
text/css \
text/html \
text/plain \
text/x-component \
text/xml
</IfModule>
</IfModule>
Source
Set an expiration header
xpires headers tell the browser whether they should request a specific file from the server or just grab it from the cache. It is advisable to set static content’s expires headers to something far in the future.
If you don’t control versioning with filename-based cache busting, consider lowering the cache time for resources like CSS and JS to something like 1 week. Source
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault “access plus 1 month”
# CSS
ExpiresByType text/css “access plus 1 year”
# Data interchange
ExpiresByType application/json “access plus 0 seconds”
ExpiresByType application/xml “access plus 0 seconds”
ExpiresByType text/xml “access plus 0 seconds”
# Favicon (cannot be renamed!)
ExpiresByType image/x-icon “access plus 1 week”
# HTML components (HTCs)
ExpiresByType text/x-component “access plus 1 month”
# HTML
ExpiresByType text/html “access plus 0 seconds”
# JavaScript
ExpiresByType application/javascript “access plus 1 year”
# Manifest files
ExpiresByType application/x-web-app-manifest+json “access plus 0 seconds”
ExpiresByType text/cache-manifest “access plus 0 seconds”
# Media
ExpiresByType audio/ogg “access plus 1 month”
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType video/mp4 “access plus 1 month”
ExpiresByType video/ogg “access plus 1 month”
ExpiresByType video/webm “access plus 1 month”
# Web feeds
ExpiresByType application/atom+xml “access plus 1 hour”
ExpiresByType application/rss+xml “access plus 1 hour”
# Web fonts
ExpiresByType application/font-woff2 “access plus 1 month”
ExpiresByType application/font-woff “access plus 1 month”
ExpiresByType application/vnd.ms-fontobject “access plus 1 month”
ExpiresByType application/x-font-ttf “access plus 1 month”
ExpiresByType font/opentype “access plus 1 month”
ExpiresByType image/svg+xml “access plus 1 month”
</IfModule>
Close eTags logo
By removing the ETag header, you disable caches and browsers from being able to validate files, so they are forced to rely on your Cache-Control and Expires header. Source
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None
Miscellaneous
Setting up a PHP variable
php_value <key> <val>
# For example:
php_value upload_max_filesize 50M
php_value max_execution_time 240
Custom Error Pages
ErrorDocument 500 “Houston, we have a problem.”
ErrorDocument 401 http://error.example.com/mordor.html
ErrorDocument 404 /errors/halflife3.html
Forced Download
Sometimes you want to the browser to download some content instead of displaying it.
<Files *.md>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
Blocking the download
Sometimes you want to ?? the browser to display some content instead of downloading it.
<FilesMatch “\.(tex|log|aux)$”>
Header set Content-Type text/plain
</FilesMatch>
Run cross-domain font reference
CDN-served webfonts might not work in Firefox or IE due to CORS. This snippet solves the problem.
<IfModule mod_headers.c>
<FilesMatch “\.(eot|otf|ttc|ttf|woff|woff2)$”>
Header set Access-Control-Allow-Origin “*”
</FilesMatch>
</IfModule>
Source
Auto UTF-8 Encode
Your text content should always be UTF-8 encoded, no?
# Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8
# UTF-8 for a number of file formats
AddCharset utf-8 .atom .css .js .json .rss .vtt .xml
Source
Switching PHP version
If you’re on a shared host, chances are there are more than one version of PHP installed, and sometimes you want a specific version for your website. For example, Laravel requires PHP >= 5.4. The following snippet should switch the PHP version for you.
AddHandler application/x-httpd-php55 .php
# Alternatively, you can use AddType
AddType application/x-httpd-php55 .php
Compatibility View in IE may affect how some websites are displayed. The following snippet should
IE to use the Edge Rendering Engine and disable the Compatibility View.
<IfModule mod_headers.c>
BrowserMatch MSIE is-msie
Header set X-UA-Compatible IE=edge env=is-msie
</IfModule>
Supports WebP image format
If WebP images are supported and an image with a .webp extension and the same name is found at the same place as the jpg/png image that is going to be served, then the WebP image is served instead.
RewriteEngine On
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
RewriteRule (.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1]
|
|
Recent Comments