Help with configuring nginx for rewrites.

Hello there!

I've just recently set up an nginx webserver after reading about it being better with resources than Apache.

Having experience with Apache before (my shared hosting uses Apache) working in the nginx environment is completely new to me o.O

I'm trying to install SpaceBukkit on my Linode for Minecraft Server management, and am struggling as to where to put:

location / {
        index  index.php /index.php;
        rewrite (.*) /app/webroot$1 ;
}

location /app/ {
        rewrite ^$ webroot/ ;
        rewrite (.*) webroot$1 ;
}

location /app/webroot/ {
        if (!-e $request_filename) {
                rewrite ^(.*)$ /index.php?$1 ;
        }
        index index.php /index.php;
}

Spacebukkit is located in a directory (sub.domain.tld/admin) of the server_name (sub.domain.tld) and adding /admin behind every tag doesn't seem to function and gives an error 500 code. How may I modify the above code to function in a directory?

Thanks!

EDIT: Also, I'd like it so that if users visit the site via another domain (such as domain.tld/minecraft/admin) they would be able to access the site with the rewrite rules applied.

It's like having a .htaccess in the directory, but since nginx doesn't support .htaccess… oh well :P

3 Replies

First. Try to avoid if and rewrite at first. Everything here can be acheived with try_files and root. When you think you need rewrite use location ~ regex {… } and see how far you get.

location / { 
        root /app/webroot;
        index  index.php /index.php; 
        try_file $url $url/ ; 
}

location /app/ { 
        root webroot ; 
        try_files $url ; 
} 

location /app/webroot/ { 
        try_files $url $url/  /index.php?$uri&$args ; 
}

Untested but you get the gist

After changing $url to $uri the configuration you submitted doesn't function: it returns me a 500 Internal Server Error. Thanks for your assistance though!

The config I used is derived from the script's webpage: http://spacebukkit.xereo.net/wiki/index … ginx_stuff">http://spacebukkit.xereo.net/wiki/index.php?title=Installing#Anerrorpagesentmehere.2CaboutthatNginx_stuff

EDIT:

I guess I wasn't specific enough. After hours upon hours of tinkering with the configurations, I am still unable to accomplish the simple task of modifying the configuration so that it works in a subdirectory.

My latest attempt, this, doesn't work at all :(

server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www/minecraft;
        index index.php;

        # Make site accessible from _
        server_name _;

        location /admin {
                index  index.php /index.php;
                rewrite (.*) /app/webroot$1 ;
        }

        location /admin/app/ {
                rewrite ^$ webroot/ ;
                rewrite (.*) webroot$1 ;
        }

        location /admin/app/webroot/ {
                if (!-e $request_filename) {
                        rewrite ^(.*)$ /index.php?$1 ;
                }
                index index.php /index.php;
        }

        location ~* \.php$ {
                try_files $uri = 404;
                include /etc/nginx/fastcgi_params;
                fastcgi_pass unix:/tmp/phpfpm.sock;
                fastcgi_index index.php;
        }
}

Seems like you are trying to setup cakephp to work via nginx?

just google cakephp on nginx. there is no rewrite really need for nginx. just point your domain root into webroot directory

….

location / {

root /var/www/example.com/public/app/webroot/;

index index.php index.html index.htm;

if (-f $request_filename) {

break;

}

….

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct