Nginx下Magento配置与性能优化
文章目标:能够成功的使Magento运行于Nginx服务器上,并对Magento的性能进行优化。
阅读前提:对Web服务器配置有一定的经验,知道Nginx和Magento是什么,假设你已经在LAMP环境中安装过Magento,本文对基础环境和Magento的安装不作介绍, 只介绍 Nginx 下的配置以及性能优化。本文所有的配置都是基于Linux环境,Windows可以参考但不一定适用。
软件版本:Nginx:0.7.63; PHP:5.2.x; Mysql: 5.0.x; Magento:1.3.2.4
正文:
一 安装Nginx+PHP+Mysql基础环境
本文不重点介绍这个基础环境的安装,请通过Google搜索相关资料,本文假设Nginx+PHP(fastcgi)+Mysql环境工作正常.
注意事项:PHP版本必须是5.2.x,不能是最新的5.3,不然Magento安装使用时会有String处理的异常出现。
关于这个问题的讨论,参考Magento论坛: http://www.magentocommerce.com/boards/viewthread/29670/P0/
二 Nginx配置文件(本文重点之一,只列出和Magento相关的,并假设Magento安装在web根目录,而不是子目录内)
server {
listen 80;
server_name www.example.com; #修改为你的服务器名
access_log logs/host.access.log main;
location / {
root /Your/Magento/RootDoc; #修改为你的Magento安装目录
index index.php index.html index.htm;
# rewrite ^(/index.php)?/minify/([^/]+)(/.*.(js|css))$ /lib/minify/m.php?f=$3&d=$2 last;
#上面的这条Rewrite规则是为了性能优化,安装fooman-speedster插件时需要的
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php last;
}
#上面的两条Rewrite规则可以确保Magento在Nginx下完成正常Rewrite工作。
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /Your/Magento/RootDoc$fastcgi_script_name; #修改为你的Magento目录夹
include fastcgi_params; #请检查fastcgi_params文件是否存在, 默认是有的
}
location /app/etc {
deny all;
}
#上面这条规则禁止访问/app/etc/目录夹,防止别人非法读取配置文件,得到密码等信息
}
如果你为Magento安装并使用了SSL证书,请增加下面一个server配置
server {
listen 443;
server_name example.com www.example.com; #同上说明
ssl on; #打开ssl
ssl_certificate /usr/local/nginx/conf/ssl/server.crt; #证书位置,如果有chain证书,把chain证书合并到主证书里
ssl_certificate_key /usr/local/nginx/conf/ssl/server.key; #key位置
#关于证书的配置,以及Chain证书的配置,请参考nginx网站:http://wiki.nginx.org/NginxHttpSslModule
location / {
root /Your/Magento/Dir; #同上说明
index index.php index.html index.htm;
#rewrite ^(/index.php)?/minify/([^/]+)(/.*.(js|css))$ /lib/minify/m.php?f=$3&d=$2 last; #同上说明
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php last;
}
#同上说明
location /app/etc {
deny all;
}
#同上说明
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param HTTPS on; #注意,这是与80端口不一样的地方, 没有这一条,会出现循环重定向问题
fastcgi_param SCRIPT_FILENAME /your/magento/dir$fastcgi_script_name; #同上说明
include fastcgi_params; #同上说明
}
}
三 Magento性能优化(本文重点之二)
Magento是很不错,但性能一直不行,但优化好,完全可以达到实用的目的,采用Nginx服务器本身也是对Magento性能的一种优化。
1 开启gzip进行带宽优化
nginx配置文件, 加入 gzip on;
注意: 如果采用了Apache服务器, 在开启Gzip优化前, 请你注意, 存在一个bug。在苹果系统Safari4 浏览器上会不断出现异常而无法正确显示网站。 这个问题也花了我不少时间, 关于更多这个问题的讨论,请参考Magento官方论坛: http://www.magentocommerce.com/boards/viewthread/34925/ , 但在nginx服务器上开启gzip后却没有这个问题出现。
2 安装fooman-speedster插件
这个插件的官方网址: www.magentocommerce.com/extension/457/fooman-speedster
这个插件的目的是把CSS和JS压缩和minify,以及把多个CSS,JS文件整合到一个文件里。我本以为目前带宽的情况下没有必要进行这类包挂gzip的优化。但在真实不断测试过程中发现还是很有必要的。 其实最重要的原因不是压缩,而是文件整合,这样可以减少浏览器端不断发出新的连接请求,就像FTP服务器一样,多个小文件和一个大文件耗时是不一样的。在没有缓存的情况下,会发现第一次打开Magento比较慢, 打开后就比较快了。如果你安装了这个插件, 这一情况将得到很大的改善。
安装好这个插件后,需要在nginx配置里增加下面这条rewrite规则:
rewrite ^(/index.php)?/minify/([^/]+)(/.*.(js|css))$ /lib/minify/m.php?f=$3&d=$2 last;
请注意这条规则的位置, 以及请注意这条规则和官方不一样,官方的是因为magento论坛格式转换而出现了一个“;”错误。
3 PHP优化
正常就是zend optimizer, APC, eAccelerator
官方优化文档里面测试比较并推荐了eAccelerator,个人感觉都一样, 上一个就行。
4 Mysql优化
由于Magento采用了InnoDB核,所以Mysql优化还是有必要的, 主要是根据服务器具体情况修改参数,参考Magento官方建议: http://www.magentocommerce.com/blog/comments/performance-is-key-notes-on-magentos-performance/
5 将magento的cache和session文件夹mount到内存里, 提高读取cache和session的速度
mount -t tmpfs -o size=256M,mode=0777 tmpfs /var/html/var/cache/
mount -t tmpfs -o size=64M,mode=0777 tmpfs /var/html/var/session/
#注意修改为你的文件夹路径,为了能够重启后生效,请在/etc/fstab文件里加入
tmpfs /var/html/var/cache/ tmpfs size=256,mode=0777 0 0
tmpfs /var/html/var/session/ tmpfs size=64,mode=0777 0 0
6 开启Magento后台里面的cache功能
这个对性能的提高很重要,不需要重新分析那些xml配置文件,一些模板文件, 但请注意,开发过程中不要开启这个功能,不然不能即时看到更新, 生产系统一定要开启。
由于Magento自带的Compile功能还是Beta版,目前发现开启后,Magento Connect 和 AW_Blog有bug出现,由于是Beta建议生产系统中不要开启,避免不必要的异常出现。
通过采用Nginx服务器和上面的6点优化,Magento的性能完全没有问题.
十月 16th, 2010 at 00:59
magento放着虚拟空间速度有点慢哦,这里的不知道有用没,不过先谢谢了。
十月 18th, 2010 at 08:32
nginx下运行mageto会比apache快一些,主要是Mageto程序比较复杂,相对国内一些开源网店系统,都会慢一点。
二月 10th, 2011 at 14:05
News Article…
Only the best resources are mentioned in this article [...]…
三月 11th, 2011 at 03:47
really agreed with what they were saying and thought I would share it with you all…
was surprised by this so thought I would share it with my readers…
三月 13th, 2011 at 14:26
really agreed with what they were saying and thought I would share it with you all…
was surprised by this so thought I would share it with my readers…
三月 13th, 2011 at 18:15
Spend some time checking over this info…
what were these guys thinking…
三月 13th, 2011 at 19:36
Sometimes you come across someones info taht you just need to share…
Great blog entry, head over and check it out…
三月 17th, 2011 at 10:22
Spend some time checking over this info…
what were these guys thinking…
三月 18th, 2011 at 09:03
Sometimes you come across someones info taht you just need to share…
Great blog entry, head over and check it out…
三月 18th, 2011 at 09:14
Sometimes you come across someones info taht you just need to share…
Great blog entry, head over and check it out…
三月 18th, 2011 at 10:12
Spend some time checking over this info…
what were these guys thinking…
三月 18th, 2011 at 20:05
Spend some time checking over this info…
what were these guys thinking…
三月 19th, 2011 at 14:17
Spend some time checking over this info…
what were these guys thinking…
三月 19th, 2011 at 14:45
Sometimes you come across someones info taht you just need to share…
Great blog entry, head over and check it out…
三月 21st, 2011 at 22:10
Sometimes you come across someones info taht you just need to share…
Great blog entry, head over and check it out…
三月 24th, 2011 at 10:44
Spend some time checking over this info…
what were these guys thinking…
三月 31st, 2011 at 09:15
Sometimes you come across someones info taht you just need to share…
Great blog entry, head over and check it out…
四月 15th, 2011 at 13:49
Spend some time checking over this info…
what were these guys thinking…
四月 26th, 2011 at 05:52
how to increase penis strength…
[…By increasing your penis size naturally you will feel great about yourself and know that there are no artificial prescriptions drugs or stimulants inside your body. The overall benefit of having a larger penis is wonderful and we can discuss some o…
四月 27th, 2011 at 09:43
how can i get a larger penis…
[…In today’s article we are going to introduce one proven method to make your member big: penis workout. The concept: When you are sexually excited your body will send more blood to penile tissue to form erection for intercourse. Thus if your penis …
四月 28th, 2011 at 06:27
get rid of erection…
[…For those of you who view adult films most will notice that the men used in those same films are largely endowed. What’s interesting to note is that early male adult film actors were of a more normal size. It was actually a surprising discovery I …
五月 2nd, 2011 at 05:19
Lose Love Handles Fast…
[...]below you’ll find my recommended links page http://www.loselovehandlesfastblog.com/links/ with some very good websites that I think you should definitely visit[...]…
五月 2nd, 2011 at 07:31
Recent Favourite Sites…
[...]below you’ll find my links page http://www.hemorrhoidtreatmentcurerelief.com/my-favourite-sites/ with some fantastic sites that I think you should go visit[...]…
五月 3rd, 2011 at 11:04
penis enlargement in south africa…
[…If you have small penis you will live miserably and probably get yourself shrouded in embarrassment and shame. But heed this call first – you can increase the size of your penis into the size you want in a simple and accepted way. So don’t lose yo…
五月 3rd, 2011 at 11:52
free natural penis male enhancement…
[…There are so many myths and out right lies around that most people have no idea how to go about getting a bigger manhood. When surveyed about the different methods virtually all men will name penis pumps and extenders and maybe pills or creams and …
五月 11th, 2011 at 06:51
best male enhancements…
[…Are you one of the many men around the world who suffer from an undersized penis? Have you tried using a penis male enlargement patch to increase your size? Well keep reading because there are a number of reasons why you should never use these patc…
五月 17th, 2011 at 11:03
natural penis male enhancement pills…
[…The foods that you eat greatly influence the growth of your body parts. This principle applies even to the growth of your penis. If you have a rather small or average penis that’s always been laughed at by your friends maybe you’re not eating the…
五月 24th, 2011 at 09:48
daily things to do to increase penis size…
[…There are many different secrets that you can learn to help your penis grow. You will find that the most popularly advertised methods such as vitamin supplements and penis pumps rarely work to give you the kind of size you need. Instead it is bette…