centos下安装Mod_Pagespeed以加速Apache和Nginx性能

Google推出apache加速模块mod_pagespeed
大多人浏览网页如果超过3秒打不开就会离开,Google为此推出了一款apache加速模块,可以有效将网页加载速度提高50%.
网页提速的问题是一个复杂多样的问题,有很多解决方法,往往关系到网站系统程序,服务器硬件,网络传输速度等方面,而Google这款加速模块简单的解决了许多复情况的问题:

•如果你的网站采用apache构建服务器,不需要对网站CMS系统进行处理即可应用
•加速模块可以自行对网络传输的html字节优化及对图象,css进入压缩优化传输
•智能缓存是一大亮点,它可以自动智能缓存,加速下载
下面介绍一下所有特点功能

优化缓存
•缓存扩展
•压缩处理CSS
•优化javascript最大限度的减少重复请求
•自动缓存CSS
•JavaScript内嵌技术
有效载荷尺寸最小化
•压缩空白
•合并头信息
•附加属性
•内建核心Javascript
•优化图像下载
•跳地非法字符
•重写优化CSS

https://developers.google.com/speed/pagespeed/module?csw=1

在CentOS 上为Apache和Nginx安装mod_pagespeed

#wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
#yum install at
#rpm -Uvh mod-pagespeed-stable_current_x86_64.rpm

出现error: Failed dependencies:
        httpd >= 2.2 is needed by mod-pagespeed-stable-1.13.35.2-0.x86_64

出现错误时使用这个加载解压mod_pagespeed模块

cd ~

if [ `getconf WORD_BIT` = '32' ] && [ `getconf LONG_BIT` = '64' ] ; then

    wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm

else

    wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.rpm

fi

 我的安装方法


mkdir mod_pagespeed
cd mod_pagespeed
rpm2cpio ../mod-pagespeed-stable_current_x86_64.rpm | cpio -idmv
# /www/server/apache/bin/apachectl  -v #查看apache版本
Server version: Apache/2.4.43 (Unix)
Server built:   Jun  2 2020 12:21:51

 cp /root/mod_pagespeed/usr/lib64/httpd/modules/mod_pagespeed_ap24.so /www/server/apache/modules/ 
#apache版本>=2.4.2,用pagespeed_ap24.so
chmod 755 /www/server/apache/modules/mod_pagespeed_ap24.so

重启httpd:
service httpd restart

查看pagespeed是否加载:
httpd -M|grep pagespeed

安装了什么Mode_Pagespeed
让我们看看系统上安装了什么软件包mod_pagespeed:

它会安装两个模块,mod_pagespeed.so为Apache 2.2的和mod_pagespeed_ap24.so为Apache的2.4。
它将安装两个主要配置文件:pagespeed.conf和pagespeed_libraries.conf(用于Debian pagespeed.load)。如果更改其中一个配置文件,则将不再自动接收将来的更新。
一个独立的JavaScript 缩小器pagespeed_js_minify用于缩小JS并为库规范化创建元数据。

配置mod_pagespeed

mod_pagespeed 需要 Apache 加载 mod_deflate(一个将 web内容gzip压缩后传输的模块)。

64位请执行:wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
 
rpm2cpio ./mod-pagespeed-stable_current_i386.rpm | cpio -idmv
./etc/cron.daily/mod-pagespeed
./etc/httpd/conf.d/pagespeed.conf
./etc/httpd/conf.d/pagespeed_libraries.conf
./usr/bin/pagespeed_js_minify
./usr/lib/httpd/modules/mod_pagespeed.so
./usr/lib/httpd/modules/mod_pagespeed_ap24.so
./var/cache/mod_pagespeed
./var/log/pagespeed
27020 blocks
cp ./usr/lib/httpd/modules/mod_pagespeed.so /usr/local/apache/modules/ #apache版本>=2.4.2,用pagespeed_ap24.so
chmod 0755 /usr/local/apache/modules/mod_pagespeed.so
vim /usr/local/apache/conf/pagespeed.conf #新建配置文件,请根据自己需求修改
LoadModule pagespeed_module /usr/local/apache/modules/mod_pagespeed.so
<IfModule pagespeed_module>
  ModPagespeed on
  ModPagespeedInheritVHostConfig on
  AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html
  ModPagespeedFileCachePath      "/var/cache/mod_pagespeed/"
  ModPagespeedFileCacheInodeLimit    500000
  ModPagespeedAvoidRenamingIntrospectiveJavascript on
  ModPagespeedEnableFilters collapse_whitespace
  <Location /mod_pagespeed_beacon>
     SetHandler mod_pagespeed_beacon
  </Location>
  <Location /mod_pagespeed_statistics>
    Order allow,deny
    Allow from localhost
    Allow from 127.0.0.1
    SetHandler mod_pagespeed_statistics
  </Location>
  ModPagespeedMessageBufferSize 100000
  
  <Location /mod_pagespeed_message>
    Allow from localhost
    Allow from 127.0.0.1
    SetHandler mod_pagespeed_message
  </Location>
</IfModule>
mkdir -p /var/mod_pagespeed/
chown -R www.www /var/mod_pagespeed/
echo 'Include conf/pagespeed.conf' >> /usr/local/apache/conf/httpd.conf
/usr/local/apache/bin/apachectl -t
Syntax OK
service httpd restart

我的配置方法

vi /www/server/apache/conf/pagespeed.conf

#LoadModule pagespeed_module /www/server/apache/modules/mod_pagespeed.so
# Attempt to load mod_version if it wasn't loaded or compiled in (eg on Debian)
<IfModule !mod_version.c>
  LoadModule version_module /www/server/apache/modules/mod_version.so
</IfModule>

<IfVersion < 2.4>
  LoadModule pagespeed_module /www/server/apache/modules/mod_pagespeed.so
</IfVersion>
<IfVersion >= 2.4.2>
  # As default pagespeed.conf configuration uses old-style ACLs using
  # 'allow' and 'deny', we load mod_access_compat in Apache 2.4. If that's a
  # problem, the blocks using that can be converted to the newer 'Require'
  # syntax.
  <IfModule !access_compat_module>
    LoadModule access_compat_module /www/server/apache/modules/mod_access_compat.so
  </IfModule>

  LoadModule pagespeed_module /www/server/apache/modules/mod_pagespeed_ap24.so 
</IfVersion>

# Only attempt to load mod_deflate if it hasn't been loaded already.
<IfModule !mod_deflate.c>
 LoadModule deflate_module /www/server/apache/modules/mod_deflate.so
</IfModule>
<IfModule pagespeed_module>
    # Turn on mod_pagespeed. To completely disable mod_pagespeed, you
    # can set this to "off".
    ModPagespeed on

    # Direct Apache to send all HTML output to the mod_pagespeed
    # output handler.
    AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html

    # If you want mod_pagespeed process XHTML as well, please uncomment this
    # line.
    # AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER application/xhtml+xml

    # The ModPagespeedFileCachePath directory must exist and be writable
    # by the apache user (as specified by the User directive).
    ModPagespeedFileCachePath            "/var/cache/mod_pagespeed/"

    # LogDir is needed to store various logs, including the statistics log
    # required for the console.
    ModPagespeedLogDir "/var/log/pagespeed"

    # The locations of SSL Certificates is distribution-dependent.
    ModPagespeedSslCertDirectory "/etc/pki/tls/certs"
    ModPagespeedSslCertFile /etc/pki/tls/cert.pem

    # If you want, you can use one or more memcached servers as the store for
    # the mod_pagespeed cache.
    # ModPagespeedMemcachedServers localhost:11211

    # A portion of the cache can be kept in memory only, to reduce load on disk
    # (or memcached) from many small files.
    # ModPagespeedCreateSharedMemoryMetadataCache "/var/cache/mod_pagespeed/" 51200

    # Override the mod_pagespeed 'rewrite level'. The default level
    # "CoreFilters" uses a set of rewrite filters that are generally
    # safe for most web pages. Most sites should not need to change
    # this value and can instead fine-tune the configuration using the
    # ModPagespeedDisableFilters and ModPagespeedEnableFilters
    # directives, below. Valid values for ModPagespeedRewriteLevel are
    # PassThrough, CoreFilters and TestingCoreFilters.
    #
    # ModPagespeedRewriteLevel PassThrough

    # Explicitly disables specific filters. This is useful in
    # conjuction with ModPagespeedRewriteLevel. For instance, if one
    # of the filters in the CoreFilters needs to be disabled for a
    # site, that filter can be added to
    # ModPagespeedDisableFilters. This directive contains a
    # comma-separated list of filter names, and can be repeated.
    #
    # ModPagespeedDisableFilters rewrite_images

    # Explicitly enables specific filters. This is useful in
    # conjuction with ModPagespeedRewriteLevel. For instance, filters
    # not included in the CoreFilters may be enabled using this
    # directive. This directive contains a comma-separated list of
    # filter names, and can be repeated.
    #
    #ModPagespeedEnableFilters rewrite_javascript,rewrite_css
    #ModPagespeedEnableFilters collapse_whitespace,elide_attributes
    #ModPagespeedEnableFilters combine_javascript,flatten_css_imports,remove_comments,trim_urls
    
    # Explicitly forbids the enabling of specific filters using either query
    # parameters or request headers. This is useful, for example, when we do
    # not want the filter to run for performance or security reasons. This
    # directive contains a comma-separated list of filter names, and can be
    # repeated.
    #
    # ModPagespeedForbidFilters rewrite_images

    # How long mod_pagespeed will wait to return an optimized resource
    # (per flush window) on first request before giving up and returning the
    # original (unoptimized) resource. After this deadline is exceeded the
    # original resource is returned and the optimization is pushed to the
    # background to be completed for future requests. Increasing this value will
    # increase page latency, but might reduce load time (for instance on a
    # bandwidth-constrained link where it's worth waiting for image
    # compression to complete). If the value is less than or equal to zero
    # mod_pagespeed will wait indefinitely for the rewrite to complete before
    # returning.
    #
    # ModPagespeedRewriteDeadlinePerFlushMs 10

    # ModPagespeedDomain
    # authorizes rewriting of JS, CSS, and Image files found in this
    # domain. By default only resources with the same origin as the
    # HTML file are rewritten. For example:
    #
    #   ModPagespeedDomain cdn.myhost.com
    #
    # This will allow resources found on http://cdn.myhost.com to be
    # rewritten in addition to those in the same domain as the HTML.
    #
    # Other domain-related directives (like ModPagespeedMapRewriteDomain
    # and ModPagespeedMapOriginDomain) can also authorize domains.
    #
    # Wildcards (* and ?) are allowed in the domain specification. Be
    # careful when using them as if you rewrite domains that do not
    # send you traffic, then the site receiving the traffic will not
    # know how to serve the rewritten content.

    # If you use downstream caches such as varnish or proxy_cache for caching
    # HTML, you can configure pagespeed to work with these caches correctly
    # using the following directives. Note that the values for
    # ModPagespeedDownstreamCachePurgeLocationPrefix and
    # ModPagespeedDownstreamCacheRebeaconingKey are deliberately left empty here
    # in order to force the webmaster to choose appropriate value for these.
    #
    # ModPagespeedDownstreamCachePurgeLocationPrefix
    # ModPagespeedDownstreamCachePurgeMethod PURGE
    # ModPagespeedDownstreamCacheRewrittenPercentageThreshold 95
    # ModPagespeedDownstreamCacheRebeaconingKey

    # Other defaults (cache sizes and thresholds):
    #
    # ModPagespeedFileCacheSizeKb          102400
    # ModPagespeedFileCacheCleanIntervalMs 3600000
    # ModPagespeedLRUCacheKbPerProcess     1024
    # ModPagespeedLRUCacheByteLimit        16384
    # ModPagespeedCssFlattenMaxBytes       102400
    # ModPagespeedCssInlineMaxBytes        2048
    # ModPagespeedCssImageInlineMaxBytes   0
    # ModPagespeedImageInlineMaxBytes      3072
    # ModPagespeedJsInlineMaxBytes         2048
    # ModPagespeedCssOutlineMinBytes       3000
    # ModPagespeedJsOutlineMinBytes        3000
    # ModPagespeedMaxCombinedCssBytes      -1
    # ModPagespeedMaxCombinedJsBytes       92160

    # Limit the number of inodes in the file cache. Set to 0 for no limit.
    # The default value if this paramater is not specified is 0 (no limit).
    ModPagespeedFileCacheInodeLimit        500000

    # Bound the number of images that can be rewritten at any one time; this
    # avoids overloading the CPU.  Set this to -1 to remove the bound.
    #
    # ModPagespeedImageMaxRewritesAtOnce      8

    # You can also customize the number of threads per Apache process
    # mod_pagespeed will use to do resource optimization. Plain
    # "rewrite threads" are used to do short, latency-sensitive work,
    # while "expensive rewrite threads" are used for actual optimization
    # work that's more computationally expensive. If you live these unset,
    # or use values <= 0 the defaults will be used, which is 1 for both
    # values when using non-threaded MPMs (e.g. prefork) and 4 for both
    # on threaded MPMs (e.g. worker and event). These settings can only
    # be changed globally, and not per virtual host.
    #
    # ModPagespeedNumRewriteThreads 4
    # ModPagespeedNumExpensiveRewriteThreads 4

    # Randomly drop rewrites (*) to increase the chance of optimizing
    # frequently fetched resources and decrease the chance of optimizing
    # infrequently fetched resources. This can reduce CPU load. The default
    # value of this parameter is 0 (no drops).  90 means that a resourced
    # fetched once has a 10% probability of being optimized while a resource
    # that is fetched 50 times has a 99.65% probability of being optimized.
    #
    # (*) Currently only CSS files and images are randomly dropped.  Images
    # within CSS files are not randomly dropped.
    #
    # ModPagespeedRewriteRandomDropPercentage 90

    # Many filters modify the URLs of resources in HTML files. This is typically
    # harmless but pages whose Javascript expects to read or modify the original
    # URLs may break. The following parameters prevent filters from modifying
    # URLs of their respective types.
    #
    # ModPagespeedJsPreserveURLs on
    # ModPagespeedImagePreserveURLs on
    # ModPagespeedCssPreserveURLs on

    # When PreserveURLs is on, it is still possible to enable browser-specific
    # optimizations (for example, webp images can be served to browsers that
    # will accept them).  They'll be served with Vary: Accept or Vary:
    # User-Agent headers as appropriate.  Note that this may require configuring
    # reverse proxy caches such as varnish to handle these headers properly.
    #
    # ModPagespeedEnableFilters in_place_optimize_for_browser

    # Internet Explorer has difficulty caching resources with Vary: headers.
    # They will either be uncached (older IE) or require revalidation.  See:
    # http://blogs.msdn.com/b/ieinternals/archive/2009/06/17/vary-header-prevents-caching-in-ie.aspx
    # As a result we serve them as Cache-Control: private instead by default.
    # If you are using a reverse proxy or CDN configured to cache content with
    # the Vary: Accept header you should turn this setting off.
    #
    # ModPagespeedPrivateNotVaryForIE on

    # Settings for image optimization:
    #
    # Lossy image recompression quality (0 to 100, -1 just strips metadata):
    # ModPagespeedImageRecompressionQuality 85
    #
    # Jpeg recompression quality (0 to 100, -1 uses ImageRecompressionQuality):
    # ModPagespeedJpegRecompressionQuality -1
    # ModPagespeedJpegRecompressionQualityForSmallScreens 70
    #
    # WebP recompression quality (0 to 100, -1 uses ImageRecompressionQuality):
    # ModPagespeedWebpRecompressionQuality 80
    # ModPagespeedWebpRecompressionQualityForSmallScreens 70
    #
    # Timeout for conversions to WebP format, in
    # milliseconds. Negative values mean no timeout is applied. The
    # default value is -1:
    # ModPagespeedWebpTimeoutMs 5000
    #
    # Percent of original image size below which optimized images are retained:
    # ModPagespeedImageLimitOptimizedPercent 100
    #
    # Percent of original image area below which image resizing will be
    # attempted:
    # ModPagespeedImageLimitResizeAreaPercent 100

    # Settings for inline preview images
    #
    # Setting this to n restricts preview images to the first n images found on
    # the page.  The default of -1 means preview images can appear anywhere on
    # the page (if those images appear above the fold).
    # ModPagespeedMaxInlinedPreviewImagesIndex -1

    # Sets the minimum size in bytes of any image for which a low quality image
    # is generated.
    # ModPagespeedMinImageSizeLowResolutionBytes 3072

    # The maximum URL size is generally limited to about 2k characters
    # due to IE: See http://support.microsoft.com/kb/208427/EN-US.
    # Apache servers by default impose a further limitation of about
    # 250 characters per URL segment (text between slashes).
    # mod_pagespeed circumvents this limitation, but if you employ
    # proxy servers in your path you may need to re-impose it by
    # overriding the setting here.  The default setting is 1024
    # characters.
    #
    # ModPagespeedMaxSegmentLength 250

    # Uncomment this if you want to prevent mod_pagespeed from combining files
    # (e.g. CSS files) across paths
    #
    # ModPagespeedCombineAcrossPaths off

    # Renaming JavaScript URLs can sometimes break them.  With this
    # option enabled, mod_pagespeed uses a simple heuristic to decide
    # not to rename JavaScript that it thinks is introspective.
    #
    # You can uncomment this to let mod_pagespeed rename all JS files.
    #
    # ModPagespeedAvoidRenamingIntrospectiveJavascript off

    # Certain common JavaScript libraries are available from Google, which acts
    # as a CDN and allows you to benefit from browser caching if a new visitor
    # to your site previously visited another site that makes use of the same
    # libraries as you do.  Enable the following filter to turn on this feature.
    #
    ModPagespeedEnableFilters canonicalize_javascript_libraries

    # The following line configures a library that is recognized by
    # canonicalize_javascript_libraries.  This will have no effect unless you
    # enable this filter (generally by uncommenting the last line in the
    # previous stanza).  The format is:
    #    ModPagespeedLibrary bytes md5 canonical_url
    # Where bytes and md5 are with respect to the *minified* JS; use
    # js_minify --print_size_and_hash to obtain this data.
    # Note that we can register multiple hashes for the same canonical url;
    # we do this if there are versions available that have already been minified
    # with more sophisticated tools.
    #
    # Additional library configuration can be found in
    # pagespeed_libraries.conf included in the distribution.  You should add
    # new entries here, though, so that file can be automatically upgraded.
    # ModPagespeedLibrary 43 1o978_K0_LNE5_ystNklf http://www.modpagespeed.com/rewrite_javascript.js

    # Explicitly tell mod_pagespeed to load some resources from disk.
    # This will speed up load time and update frequency.
    #
    # This should only be used for static resources which do not need
    # specific headers set or other processing by Apache.
    #
    # Both URL and filesystem path should specify directories and
    # filesystem path must be absolute (for now).
    #
    # ModPagespeedLoadFromFile "http://example.com/static/" "/var/www/static/"


    # Enables server-side instrumentation and statistics.  If this rewriter is
    # enabled, then each rewritten HTML page will have instrumentation javacript
    # added that sends latency beacons to /mod_pagespeed_beacon.  These
    # statistics can be accessed at /mod_pagespeed_statistics.  You must also
    # enable the mod_pagespeed_statistics and mod_pagespeed_beacon handlers
    # below.
    #
    # ModPagespeedEnableFilters add_instrumentation

    # The add_instrumentation filter sends a beacon after the page onload
    # handler is called. The user might navigate to a new URL before this. If
    # you enable the following directive, the beacon is sent as part of an
    # onbeforeunload handler, for pages where navigation happens before the
    # onload event.
    #
    # ModPagespeedReportUnloadTime on

    # Uncomment the following line so that ModPagespeed will not cache or
    # rewrite resources with Vary: in the header, e.g. Vary: User-Agent.
    # Note that ModPagespeed always respects Vary: headers on html content.
    # ModPagespeedRespectVary on

    # Uncomment the following line if you want to disable statistics entirely.
    #
    # ModPagespeedStatistics off

    # These handlers are central entry-points into the admin pages.
    # By default, pagespeed_admin and pagespeed_global_admin present
    # the same data, and differ only when
    # ModPagespeedUsePerVHostStatistics is enabled.  In that case,
    # /pagespeed_global_admin sees aggregated data across all vhosts,
    # and the /pagespeed_admin sees data only for a particular vhost.
    #
    # You may insert other "Allow from" lines to add hosts you want to
    # allow to look at generated statistics.  Another possibility is
    # to comment out the "Order" and "Allow" options from the config
    # file, to allow any client that can reach your server to access
    # and change server state, such as statistics, caches, and
    # messages.  This might be appropriate in an experimental setup.
    <Location /pagespeed_admin>
        Order allow,deny
        Allow from localhost
        Allow from 127.0.0.1
        SetHandler pagespeed_admin
    </Location>
    <Location /pagespeed_global_admin>
        Order allow,deny
        Allow from localhost
        Allow from 127.0.0.1
        SetHandler pagespeed_global_admin
    </Location>

    # Enable logging of mod_pagespeed statistics, needed for the console.
    ModPagespeedStatisticsLogging on

    # Page /mod_pagespeed_message lets you view the latest messages from
    # mod_pagespeed, regardless of log-level in your httpd.conf
    # ModPagespeedMessageBufferSize is the maximum number of bytes you would
    # like to dump to your /mod_pagespeed_message page at one time,
    # its default value is 100k bytes.
    # Set it to 0 if you want to disable this feature.
    ModPagespeedMessageBufferSize 100000
</IfModule>

cd /www/server/apache/conf

vi httpd.conf

在最后一行添加

Include /www/server/apache/conf/pagespeed.conf

  1. mkdir -p /var/cache/mod_pagespeed/

  2. chown -R www.www /var/cache/mod_pagespeed/

  3. 重新启动apache

在Apache中,mod_pagespeed在安装时会自动打开,而在Nginx中,您需要将以下行放在nginx.conf文件中以及启用了PageSpeed的每个服务器块中:

pagespeed on;
#Needs to exist and be writable by nginx. Use tmpfs for best performance.
pagespeed FileCachePath /var/ngx_pagespeed_cache;
#Ensure requests for pagespeed optimized resources go to the pagespeed handler
#and no extraneous headers get set.
location ~ “.pagespeed.([a-z].)?[a-z]{2}.[.]{10}.[.]+” {
add_header “” “”;
}
location ~ “^/pagespeed_static/” { }
location ~ “^/ngx_pagespeed_beacon$” { }

最后,不要忘记重启Apache或Nginx服务器以启动mod_pagespeed正常工作。

第4步:验证Mod_Pagespeed模块
要验证mod_pagespeed模块,我们将使用curl命令在域或IP上进行测试,如下所示:

#curl -D- http://www.xxx.com/ | less

Apache:

HTTP/1.1 200 OK
Date: Fri, 04 Mar 2016 07:37:57 GMT
Server: Apache/2.4.6 (CentOS) PHP/5.4.16

X-Mod-Pagespeed: 1.9.32.13-0

Nginx:

HTTP/1.1 200 OK
Date: Fri, 04 Mar 2016 07:37:57 GMT
Server: nginx/1.4.0

X-Page-Speed: 1.5.27.1-2845

如果您没有看到X-Mod-Pagespeed标头,则表示实际上并未安装mod_pagespeed。

步骤5:打开和关闭模块
如果你不希望使用的mod_pagespeed完全,你可以关闭通过插入下面的行pagespeed.conf顶部文件。

ModPagespeed off

同样,要打开模块,请将以下行插入顶部的pagespeed.conf文件中。

ModPagespeed on

正如我在安装mod_pagespeed后所说的那样,我们的网站加载速度提高了40%-50%。我们非常想通过评论在您的系统上安装后了解您的网站速度。

有关配置的更多详细信息,请访问[https://developers.google.com/speed/pagespeed/module/]查看官方mod_pagespeed页面。

使用nginx的优点

1、可以高并发连接

          官方测试Nginx能够支撑5万并发连接,实际生产环境中可以支撑2~4万并发连接数。

          原因,主要是Nginx使用了最新的epoll(Linux2.6内核)和kqueue(freeBSD)网路I/O模型,而Apache使用的是传统的Select模型,其比较稳定的Prefork模式为多进程模式,需要经常派生子进程,所以消耗的CPU等服务器资源,要比Nginx高很多。

 

          2、内存消耗少

          Nginx+PHP(FastCGI)服务器,在3万并发连接下,开启10个Nginx进程消耗150MB内存,15MB*10=150MB,开启的64个PHP-CGI进程消耗1280内存,20MB*64=1280MB,加上系统自身消耗的内存,总共消耗不到2GB的内存。

          如果服务器的内存比较小,完全可以只开启25个PHP-CGI进程,这样PHP-CGI消耗的总内存数才500MB。

 

          3、成本低廉

          购买F5BIG-IP、NetScaler等硬件负载均衡交换机,需要十多万到几十万人民币,而Nginx为开源软件,采用的是2-clause BSD-like协议,可以免费试用,并且可用于商业用途。

          BSD开源协议是一个给使用者很大自由的协议,协议指出可以自由使用、修改源代码、也可以将修改后的代码作为开源或专用软件再发布。

 

          4、配置文件非常简单

          网络和程序一样通俗易懂,即使,非专用系统管理员也能看懂。

          5、支持Rewrite重写

          能够根据域名、URL的不同,将http请求分到不同的后端服务器群组。

          6、内置的健康检查功能

          如果NginxProxy后端的某台Web服务器宕机了,不会影响前端的访问。

          7、节省带宽

          支持GZIP压缩,可以添加浏览器本地缓存的Header头。

          8、稳定性高

          用于反向代理,宕机的概率微乎其微。

          9、支持热部署

          Nginx支持热部署,它的自动特别容易,并且,几乎可以7天*24小时不间断的运行,即使,运行数个月也不需要重新启动,还能够在不间断服务的情况下,对软件版本进行升级。

          Nginx、Apache、Lighttpd对比

          可以看出,Nginx在反向代理、Rewrite规则、稳定性、静态文件处理,内存消耗等方面,有很强的优势,使用Nginx取代传统的Apache服务器,会得到多方面的性能提升。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是可以的。您可以通过在CentOS 8上安装ApacheNginx来实现此目的。首先,确保您的系统已启用EPEL存储库。然后,按照以下步骤进行操作: 1. 安装ApacheNginx 使用以下命令安装ApacheNginx: ``` sudo dnf install httpd nginx ``` 2. 配置Apache 默认情况下,Apache监听端口80。您可以通过编辑`/etc/httpd/conf/httpd.conf`文件来更改Apache的默认设置。找到以下行: ``` Listen 80 ``` 将其更改为: ``` Listen 127.0.0.1:8080 ``` 这将使Apache仅在本地侦听端口8080。 3. 配置Nginx 默认情况下,Nginx监听端口80。您可以通过编辑`/etc/nginx/nginx.conf`文件来更改Nginx的默认设置。找到以下行: ``` listen 80 default_server; ``` 将其更改为: ``` listen 127.0.0.1:8081 default_server; ``` 这将使Nginx仅在本地侦听端口8081。 4. 配置虚拟主机 现在,您需要为ApacheNginx分别配置虚拟主机。在Apache中,您可以通过编辑`/etc/httpd/conf.d/vhosts.conf`文件来添加虚拟主机。例如: ``` <VirtualHost *:8080> ServerAdmin webmaster@localhost DocumentRoot /var/www/html/apache ServerName apache.example.com ErrorLog logs/apache-error.log CustomLog logs/apache-access.log combined </VirtualHost> ``` 在Nginx中,您可以通过编辑`/etc/nginx/conf.d/vhosts.conf`文件来添加虚拟主机。例如: ``` server { listen 127.0.0.1:8081; server_name nginx.example.com; location / { root /var/www/html/nginx; index index.html; } } ``` 5. 启动ApacheNginx 使用以下命令启动ApacheNginx: ``` sudo systemctl start httpd sudo systemctl start nginx ``` 现在,您可以通过访问`http://127.0.0.1:8080`来访问Apache,并通过访问`http://127.0.0.1:8081`来访问Nginx

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值