[英]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.