我的任务是pipe理一个带有一堆遗留代码的基础目录的Web应用程序,以及我自己制作的一个子目录,我们正在使用这个API来缓慢地将前端迁移到SPA模型。 主人已经得到了野兔的屁股,并下令将我们移动到Nginx。
我在Apache重写方面有丰富的经验,我需要大约9秒的时间才能在Apache中进行设置。 然而,正如人们很快指出的那样,nginx不是Apache。 虽然我可以让应用程序的任何一部分在nginx中自行工作,但是当它位于/ api /目录中时,我无法让API与主应用程序一起玩。
我猜测这将是位置指令的一些排列,这似乎在Apache中的行为类似。 基本上,如果有什么命中/ api /目录,我需要通过重写将uri的其余部分传递给/api/public/index.php:
^/(.*)$ public/index.php?_url=/$1;
任何帮助是极大的赞赏。
现有configuration:
server { listen 10.1.1.225:443 ssl; server_name dev.domain.com; ssl_certificate /etc/nginx/conf.d/chain.crt; ssl_certificate_key /etc/nginx/conf.d/4096_SSL.key; set $root_path '/usr/share/nginx/www'; root $root_path/trunk; index index.php index.html; fastcgi_param ENV development; fastcgi_buffers 8 16k; fastcgi_buffer_size 32k; fastcgi_read_timeout 180; location / { try_files $uri $uri/ =404; } location ^~ /api { rewrite ^/(.*)$ public/index.php?_url=/$1; } fastcgi_intercept_errors off; location ~ \.php { fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
这个configuration是丑陋的,因为我一整天都在玩它,但它应该得到重点。
你说得对,nginx不是apache,这意味着你可能会想到使用重写的大部分地方都是不恰当的。 相反,你应该尝试使用try_files 。 例如:
location /api/ { try_files $uri /api/public/index.php?_url=$request_uri; }
对于其他人在这个取代:
location ^~ /api { rewrite ^/(.*)$ public/index.php?_url=/$1; }
同
rewrite ^/api(/.*)$ /api/public/index.php?_url=$1 last;