简体   繁体   English

从Apache到nginx迁移,重写.htaccess问题

[英]Apache to nginx migration, rewrite problems with .htaccess

I'm trying to migrate my php web application from Apache to Nginx. 我正在尝试将php Web应用程序从Apache迁移到Nginx。 And I have a huge problem with converting rewrite rules. 我在转换重写规则方面遇到了很大的问题。

I have used several converters. 我已经使用了几个转换器。 Even know how to convert it myself, but at the end I'm finish with pretty same result. 甚至我自己都知道如何进行转换,但是最后我得到了几乎相同的结果。 Default page is downloading php source code instead of showing a page. 默认页面是下载php源代码,而不是显示页面。

If I set root directly to public folder and remove rewrite rules. 如果我直接将root设置为公用文件夹并删除重写规则。 Default page is displaying but without css, images and with broken links. 默认页面显示,但没有CSS,图像和链接断开。 But this is sort of forseen because the same happens locally (with Apache) if I set DocumentRoot to public and delete .htacces files. 但这是可以预见的,因为如果我将DocumentRoot设置为public并删除.htacces文件,则在本地(使用Apache)也会发生这种情况。

PHP is using Phalcon framework. PHP正在使用Phalcon框架。 So *.volt files are compiled to *.volt.php during "php runtime". 因此,*。volt文件在“ php运行时”期间被编译为* .volt.php。

Here is my application dir tree. 这是我的应用程序目录树。

<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

In config.in baseUri = / 在config.in baseUri = /

Htaccesses Htaccesses

/.htaccess /.htaccess

RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ((?s).*) public/$1 [L]

/public/.htaccess /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]

This i my nginx configuration. 这是我的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;
    }
}

I would be grateful for any help. 我将不胜感激。

You need to rewrite your URIs and add a /public prefix. 您需要重写URI并添加/public前缀。 This affects all of your files, including PHP files, and can be solved by using nested location blocks. 这会影响您的所有文件,包括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; }

I am assuming the /app path is never accessed. 我假设从未访问过/app路径。 If it is, the location / needs modification. 如果是,则location /需要修改。

Your location ~ \\.php(/|$) block looks a little strange. 您的location ~ \\.php(/|$)块看起来有些奇怪。 It accepts scripts with path info but then fails to break it down. 它接受带有路径信息的脚本,但无法对其进行分解。 Maybe this works with your particular PHP implementation. 也许这适用于您的特定PHP实现。

The alternative approach is to set the root to .../public and identify which rewrite rules are necessary to make the rogue URIs work. 另一种方法是将根目录设置为.../public并确定使恶意URI起作用所需的重写规则。 Perhaps removing a leading /public prefix. 也许删除前导/public前缀。

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

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