简体   繁体   中英

Migrating from apache to nginx

So I've been using apache for all of my PHP projects, but now I've been forced to using nginx on the server. I've no clue how to do pretty urls with nginx. So I started converting my .htaccess file into an nginx config script, and tried modifying the nginx config file, but no luck.

This is my .htaccess file:

Options -MultiViews
RewriteEngine On

RewriteBase /wallfly-mvc/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

This is my nginx.conf server block:

server {
    listen 80; listen [::]:80;
    server_name  deco3801-superflyz.uqcloud.net;
    root         /var/www/htdocs;

    include "set_cookie.conf";
    rewrite_by_lua_file "etc/nginx/lua/auth_filter.lua";

    location / {
        index  index.html index.htm index.php index.jsp index.aspx;
        try_files $uri $uri/ =404;
    }

    location /wallfly-mvc/public/ {
        if (!-e $request_filename) {
            rewrite ^/wallfly-mvc/public/(.+)$ /wallfly-mvc/public/index.php?url=$1 break;
        }
    }
}

I want every request to point to public/index.php . This is my project structure :

wallfly-mvc/
  app/
  public/
    index.php
    css/
    js/
    img/

How should I change the nginx.conf file? Any help would be appreciated.

You can use location ^~ to capture the directory. Its priority is higher location /

server {
    #...

    location ^~ /wallfly-mvc/public/ {
        if (!-e $request_filename) {
            rewrite ^/wallfly-mvc/public/(.+)$ /wallfly-mvc/public/index.php?url=$1 break;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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