[英]Apache to nginx migration, rewrite problems with .htaccess
我正在尝试将php Web应用程序从Apache迁移到Nginx。 我在转换重写规则方面遇到了很大的问题。
我已经使用了几个转换器。 甚至我自己都知道如何进行转换,但是最后我得到了几乎相同的结果。 默认页面是下载php源代码,而不是显示页面。
如果我直接将root设置为公用文件夹并删除重写规则。 默认页面显示,但没有CSS,图像和链接断开。 但这是可以预见的,因为如果我将DocumentRoot设置为public并删除.htacces文件,则在本地(使用Apache)也会发生这种情况。
PHP正在使用Phalcon框架。 因此,*。volt文件在“ php运行时”期间被编译为* .volt.php。
这是我的应用程序目录树。
<repo>
├── app
│ ├── config
│ │ ├── autoloader.php
│ │ ├── config.ini
│ │ └── services.php
│ ├── controllers
│ │ ├── CategoryController.php
│ │ ├── ControllerBase.php
│ │ ├── IndexController.php
│ │ ├── LoginController.php
│ │ ├── ProductController.php
│ │ ├── ProfileController.php
│ │ ├── RankController.php
│ │ └── SignupController.php
│ ├── library
│ │ └── Elements.php
│ ├── models
│ │ ├── Category.php
│ │ ├── CurlHandler.php
│ │ ├── OpinionVote.php
│ │ ├── Product.php
│ │ ├── REST.php
│ │ ├── Users.php
│ │ └── Validator.php
│ └── views
│ ├── category
│ │ └── show.volt
│ ├── index
│ │ └── index.volt
│ ├── index.volt
│ ├── layouts
│ │ ├── footer.volt
│ │ ├── leftColumn.volt
│ │ ├── logo.volt
│ │ └── topMenu.volt
│ ├── login
│ │ └── index.volt
│ ├── product
│ │ └── show.volt
│ ├── profile
│ │ ├── addCategory.volt
│ │ ├── addProduct.volt
│ │ ├── deleteCategory.volt
│ │ └── index.volt
│ ├── rank
│ │ ├── bottom.volt
│ │ ├── index.volt
│ │ └── top.volt
│ └── signup
│ └── index.volt
├── public
│ ├── css
│ │ └── main.css
│ ├── img
│ │ └── oceniarka-logo.png
│ ├── index.php
│ ├── scripts
│ │ └── opinionVoteJs.js
│ └── signup.php
└── README.md
在config.in baseUri = /
Htaccesses
/.htaccess
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ((?s).*) public/$1 [L]
/public/.htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^.*/img/([a-zA-Z0-9-]+)[.]png$ img/$1.png [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
这是我的Nginx配置。
server {
listen ${OPENSHIFT_DIY_IP}:${OPENSHIFT_DIY_PORT};
server_name localhost;
root ${OPENSHIFT_REPO_DIR};
add_header Strict-Transport-Security max-age=691200;
location / {
index index.php index.html index.htm;
rewrite ^/$ /public/ break;
rewrite ((?s).*) /public/$1 break;
}
try_files $uri $uri/ @rewrites;
location /public {
rewrite ^/.*/img/([a-zA-Z0-9-]+)[.]png$ /img/$1.png last;
if (!-d $request_filename) {
set $rule_1 1$rule_1;
}
if (!-f $request_filename) {
set $rule_1 2$rule_1;
}
if ($rule_1 = "21") {
rewrite ^/((?s).*)$ /index.php?_url=/$1 last;
}
}
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~ ~$ { access_log off; log_not_found off; deny all; }
# Set expires max on static file types
location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$ {
access_log off;
log_not_found off;
# Some basic cache-control for static files to be sent to the browser
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location @rewrites {
rewrite ^(.*)$ /index.php?_url=/$1;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ ^/index\.php(/|$) {
fastcgi_pass unix:${OPENSHIFT_RUN_DIR}/php-fpm.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include fastcgi_params;
}
}
我将不胜感激。
您需要重写URI并添加/public
前缀。 这会影响您的所有文件,包括PHP文件,并且可以使用嵌套的位置块来解决。
root ...;
index index.php index.html index.htm;
location / {
rewrite ^ /public$request_uri last;
}
location /public {
try_files $uri $uri/ /public/index.php?_url=$uri&$args;
location ~ \.php(/|$) {
...
}
location ~* \.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$ {
rewrite (/img/.+\.png)$ /public$1 break;
...
}
}
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~ ~$ { access_log off; log_not_found off; deny all; }
我假设从未访问过/app
路径。 如果是,则location /
需要修改。
您的location ~ \\.php(/|$)
块看起来有些奇怪。 它接受带有路径信息的脚本,但无法对其进行分解。 也许这适用于您的特定PHP实现。
另一种方法是将根目录设置为.../public
并确定使恶意URI起作用所需的重写规则。 也许删除前导/public
前缀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.