使用 mod_rewite 和 htaccess 从 Apache 切换到 nginx

使用 mod_rewite 和 htaccess 从 Apache 切换到 nginx

我想使用 mod_rewite 和 htaccess 从 Apache 切换到 Nginx。我该怎么做?

我的 mod_rewite 设置:

    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /xxxxxx/web/>
            Options -Indexes +FollowSymLinks
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>

和我的htaccess:

Options +FollowSymLinks +ExecCGI

<IfModule mod_php5.c>
  php_value mbstring.detect_order       auto
  php_value mbstring.internal_encoding  UTF-8
  php_value mbstring.func_overload      7

  php_value session.gc_probability      1
  php_value session.gc_divisor          100
  php_value session.gc_maxlifetime      1440
</IfModule>

AddType video/x-flv .flv
AddType video/mp4 .mp4 .m4v
AddType video/ogg .ogg .ogv
AddType video/webm .webm

AddType audio/mpeg .mp3
AddType audio/ogg .oga
AddType audio/x-wav .wav

AddType application/pdf .pdf
AddType application/rtf .rtf
AddType text/plain .txt

AddType application/msword .doc
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.document .docx
AddType application/vnd.ms-excel .xls
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet .xlsx
AddType application/vnd.ms-powerpoint .ppt
AddType application/vnd.openxmlformats-officedocument.presentationml.presentation .pptx

AddType application/x-shockwave-flash .swf

AddType application/vnd.ms-fontobject .eot
AddType application/octet-stream .otf .ttf
AddType application/x-woff .woff
AddType application/x-font-woff .woff
AddType application/font-woff .woff

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 week"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType application/javascript "access plus 1 year"

  <IfModule mod_headers.c>
    <FilesMatch ".(jpg|jpeg|png|gif|js|css)(\?.*)?$">
      FileETag All
      Header set Cache-Control "public, max-age=31536000"
    </FilesMatch>
  </IfModule>

  SetEnvIf Request_URI .*/flashcomm IS_FLASHCOMM
  <IfModule mod_headers.c>
      Header set Cache-Control "no-cache, no-store" env=IS_FLASHCOMM
      Header unset Expires env=IS_FLASHCOMM
  </IfModule>
  UnsetEnv IS_FLASHCOMM
</IfModule>


<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript image/png image/gif image/jpeg

  ## Insert filter
  #SetOutputFilter DEFLATE
  #
  ## Netscape 4.x has some problems...
  #BrowserMatch ^Mozilla/4 gzip-only-text/html
  #
  ## Netscape 4.06-4.08 have some more problems
  #BrowserMatch ^Mozilla/4\.0[678] no-gzip
  #
  ## MSIE masquerades as Netscape, but it is fine
  ## BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  #
  ## NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
  ## the above regex won't work. You can use the following
  ## workaround to get the desired effect:
  #BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
  #
  #SetEnvIfNoCase Request_URI \.(og[gav]|mp4|m4[av]|webm|mp3|wav).*$ no-gzip dont-vary
  #
  ## disable gzip compression for media gate
  #SetEnvIfNoCase Request_URI ^/getMedium/.* no-gzip dont-vary
  #SetEnvIfNoCase Request_URI ^/cache/.*\.mp4 no-gzip dont-vary
  #SetEnvIfNoCase Request_URI ^/cache/.*\.m4v no-gzip dont-vary

  <IfModule mod_headers.c>
    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary
  </IfModule>
</IfModule>

<IfModule mod_rewrite.c>
  RewriteEngine On

  # uncomment the following line, if you are having trouble
  # getting no_script_name to work
  #RewriteBase /

  # Backend Application Rules
  RewriteCond %{REQUEST_URI} ^/backend$
  RewriteRule ^backend$ backend.php/ [QSA,L,PT,NC]
  RewriteCond %{REQUEST_URI} ^/backend/
  RewriteRule ^backend/(.*)$ backend.php/$1 [QSA,L,PT,NC]

  # WebTV Application Rules
  RewriteCond %{REQUEST_URI} ^/webtv$
  RewriteRule ^webtv$ webtv.php/ [QSA,L,PT,NC]
  RewriteCond %{REQUEST_URI} ^/webtv/
  RewriteRule ^webtv/(.*)$ webtv.php/$1 [QSA,L,PT,NC]

  # we skip all files with .something
  #RewriteCond %{REQUEST_URI} \..+$
  #RewriteCond %{REQUEST_URI} !\.html$
  #RewriteRule .* - [L]

  # we check if the .html version is here (caching)
  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+[^/])$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d

  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

答案1

  1. 阅读 Apache 和 mod rewrite 的文档,了解您使用的指令的作用。搜索引擎也是您的朋友。

  2. 使用 nginx 文档来了解 apache/mod rewrite 的功能如何执行。搜索引擎也是您的朋友。

  3. 根据您的生产环境构建测试环境。

  4. 根据上面1和2的分析制定测试配置。

  5. 将测试配置应用到测试环境。

  6. 测试新配置

  7. 分析测试结果。

  8. 根据需要重复 4 到 7 以确保配置正确。

  9. 使用记录良好的变更流程将变更应用于生产。

什么不该做。

1. 在互联网网站上转储一堆配置并希望获得最佳结果。

相关内容