简体   繁体   English

Nginx配置两个PHP版本

[英]Nginx configuration for two versions of PHP

I'm trying to set up an nginx environment where legacy code and new MVC-style code can co-exist, so that I can gradually refactor it page by page. 我正在尝试建立一个Nginx环境,在该环境中,旧代码和新的MVC样式代码可以共存,以便逐步逐步重构它。 The legacy code needs an older version of PHP (it runs best on 5.3, but I had trouble compiling that, so I went with 5.4 and will fix anything that breaks), but it is easily distinguishable by URL, because it has literal file names like http://sub.domain.com/search.php?category=4 , etc. instead of new style like http://sub.domain.com/search/category/4 - the key difference is the presence of .php . 遗留代码需要较旧版本的PHP(它在5.3上运行最佳,但是我在编译时遇到了麻烦,因此我使用5.4并将修复所有可能损坏的内容),但是可以通过URL轻松区分,因为它具有文字文件名例如http://sub.domain.com/search.php?category=4等,而不是像http://sub.domain.com/search/category/4这样的新样式-关键的区别在于.php

The new code runs fine with the following in the nginx config: 新代码可以在nginx配置中正常运行,并具有以下内容:

server {
  listen 80;
  server_name *.myproject.dev;
  root /var/www/myproject/public;
  index index.php index.html index.htm;
  try_files $uri $uri/ @rewrite;
  location @rewrite {
    rewrite ^/(.*)$ /index.php?_url=/$1;
  }
  location ~ ^(.+\.php)(/.*)?$ {
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
  }
}

(I will admit that I don't completely understand all that code - it came from various guides and such.) (我承认我不完全理解所有代码,它来自各种指南等。)

With the help of this great tutorial I compiled and installed PHP 5.4 in its own location listening on port 9001. It works fine using a separate domain for the old code, but what I want to do is use a single domain, but call the old code if .php is found in the URL, and do the requisite rewrite on anything else and use the new code. 这个出色的教程的帮助下,我在自己的位置上编译并安装了PHP 5.4,侦听端口9001。使用旧代码的单独域可以很好地工作,但是我想做的是使用单个域,但是将旧域称为如果在URL中找到了.php则进行代码编码,然后对其他任何内容进行必要的重写并使用新代码。 I found this post on ServerFault and tried incorporating its ideas in my situation like this: 在ServerFault上找到了这篇文章,并尝试将它的想法结合到我的情况中,如下所示:

server {
  listen 80;
  server_name *.myproject.dev;
  root /var/www/myproject/public;
  index index.php index.html index.htm;
  try_files $uri $uri/ @rewrite;
  location @rewrite {
    rewrite ^/(.*)$ /index.php?_url=/$1;
  }
  location ~ ^(.+\.php)(/.*)?$ {
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;

    # Anything with ".php" is directed to the old codebase
    location ~* \.php {
      root /var/www/myproject/oldcode;
      fastcgi_pass 127.0.0.1:9001;
    }
  }
}

But the rewrite adds index.php to the new code, so in the end, everything matches the .php test, which is not the intent. 但是重写将index.php添加到了新代码中,因此最终,所有内容都与.php测试匹配,这并非意图。 I tried putting those final four lines earlier in the file with several variations, but that didn't help (either a blank page or still only going to the old code location, depending on the details). 我试着将最后四行放在文件的前面,有几种变体,但这无济于事(要么是空白页,要么仍然只转到旧代码位置,具体取决于细节)。 Does someone know enough about nginx config syntax to help me rearrange it so that it does what I want? 有人对nginx config语法了解得足够多,可以帮助我重新排列它,以便它满足我的要求吗?

If your new code only uses /index.php and without any path_info, you could use a prefix location: 如果新代码仅使用/index.php而没有任何path_info,则可以使用前缀位置:

location ^~ /index.php { ... }
location ~* \.php { ... }

The first location takes precedence due to the ^~ operator. 由于使用^~运算符,因此第一个位置具有优先权。 Or an exact match (which also takes precedence): 或完全匹配(也优先):

location = /index.php { ... }
location ~* \.php { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM