nginx URL-rewrite dynamic for all subfolders - apache

I am facing the following problem when migrating from apache to nginx:
When deploying a folder named "apps" including about 10 subfolders with zend framework 2 apps, I am not able to browse my zf-routes (http 404).
This happens because my htaccess is not read and my rewrite does not take place.
It would work when defining all projects in my nginx config but this is not very dynamic and all new projects would need a new entry while apache read the .htaccess in all folders and did it on itself.
Is there any nginx directive I did not find to accomplish this?
I am currently trying this codeblock:
location /apps/ {
try_files $uri $uri/ /index.php;
}
I know this would cause nginx to check /apps/index.php instead of /apps/(projectname)/index.php - but I don't know how to change this.

location /apps/(.*)/ {
try_files $uri $uri/ /apps/(.*)/index.php?$args;
}
This is the solution. I just missed the part before index.php.

Related

Avoid Nginx to add '/' before query parameters

I'm currently developing a Nuxt SPA that will be served with Nginx.
When I run de nuxt server localy, everything is working fine. But when runing the app on nginx, I have an issue when adding query parameters.
Actually nginx automatically rewrite all URL to add a trailing /. Thus, when add query parameter I am automatically redirected to: www.exemple.com/my-path/?my-query-param=1234.
But what I want is to access www.exemple.com/my-path?my-query-param=1234 without the /?.
I tried adding rewrite ^/(.*)/$ /$1; to my nginx conf, unfortunately, this make me fall in a redirection loop (? -> /? -> ? -> /? -> ...).
my current nginx conf is very basic:
server {
listen 80;
index index.html;
include /etc/nginx/mime.types;
}
Can someone help me to fix my issue ?
Thanks in advance.
UPDATE:
I also tried try_files $uri $uri/index.html =404; without any success :-(

Nuxt.js app behind nginx reverse-proxy loading multiple pages at once

I've a nuxt.js app behind nginx reverse proxy. The nginx conf file looks like this:
server {
listen 80;
# Match *.lvh.me
server_name ~^(?<user>.+)\.lvh\.me$;
location / {
proxy_pass http://localhost:8080/sites/$user$uri$is_args$args;
}
location ~* \.(?:js|css|jpg|jpeg|gif|png|ico|cur|svg)$ {
proxy_pass http://localhost:8080;
}
}
As you can see I'm mapping all my site subdomains to a specific path on my site and it is working fine. I'm also mapping all assets to be loaded from the root (because otherwise it throws a 404 error).
The only issue I'm facing is the whenever I visit a subdomain e.g subdomain.lvh.me it loads two pages on top of each other, the original page from subdomain root (which is expected) but also the page from the main domain root i.e. lvh.me (which is not expected).
Can you please checkout my conf file to see if I'm doing anything wrong here?
So I've encountered this issue and what I did to fix it was to not rely on Nginx's root nor proxy_pass. Instead, I used a location block with an alias and a try_files inside like so:
location ^~ / {
alias /path/to/dist;
try_files $uri $uri/ /index.html = 404;
}

nginx or apache how to serve content from /subfolder no matter what url you are at

I have an frontend app, written in Angular, hosted at http://localhost/subfolder/myapp
When I go to the app, it redirects to route1, the url appears as:
http://localhost/subfolder/myapp/route1
But when I refresh the browser, I get 404 not found error. I think that's because there is no physical index.html in /subfolder/myapp/route1 and /subfolder/myapp/route1 is not a physical folder.
So I wrote a rewrite rule
rewrite ^/subfolder/myapp/.+$ /subfolder/myapp last;
But since when I go to http://localhost/subfolder/myapp I am redirected to http://localhost/subfolder/myapp/route1, and the rule is redirecting me back to http://localhost/subfolder/myapp, I am in an infinite redirect state (too many redirect problem)
What can I do in nginx to solve my problem? Or what about Apache?
Figured this out:
location /subfolder/myapp/ {
try_files $uri /subfolder/myapp/index.html;
}
location /subfolder2/myapp/ {
try_files $uri /subfolder2/myapp/index.html;
}

nginx: try to serve minified files first

I'm trying to optimize my websites by minifying the css, js, svg and whatnot.
This however meddles with my codebase and I would like to keep the original files around. I've set up git to autmoatically minify any css|js|svg to min.css|min.js|min.svg but I'm not editing every php or html file that links to these files to use the .min. extension.
To mend this, I was thinking that I could set up nginx serve the minified versions of the files automatically when a request for the non-minified files is done using the try_files directives and use the non-minified files as a backup if they don't exist.
This is some pseudo-configuration to elaborate what I mean:
location \.(css|js|svg) {
try_files $minified_uri $uri;
}
but I have no clue on how to get the $minified_uri here (e.g. /some/style.min.css is served when /some/style.css is requested).
Also, if you think this approach is really bad, please be free to tell me why and show me some alternatives!
This is fairly easy to accomplish using regex variables:
location ~ ^(.+)\.(css|js|svg)$ {
try_files $1.min.$2 $uri =404;
}
I don't guarantee that it would work but I guess you can give it a try
location ~ (?<name>[^/]*)\.(?<extention>css|js|svg) {
try_files $name.min.$extention $name.$extention;
}

Not found other than homepage

I'm using YII framework. I can access my site through: localhost/index.php
then If I click any links on it it says: 404 not found.
It works on Apache. I'm trying to configure it with NGINX with no success. Can somebody please tell me what can be the problem if something works with Apache but does not work with NGINX?
Log error from nginx:
2011/05/07 11:27:42 [error] 5104#3152: *30 CreateFile() "c:\EWemp\nginx-0.8.52/html/rooms/finished" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /rooms/finished HTTP/1.1", host: "localhost", referrer: "http://localhost/index.php"
So, I assume that it needs some kind of URL rewrite, since I do not have html/rooms/finished directory.
It is like html/controller/action/ but I do not know what to change in order to get it to work
Yii uses one index.php file to handle all client requests. You need to rewrite /rooms/finished to index.php/rooms/finished.
I have used this Nginx configuration to rewrite all requests to be handled by one index.php file. This configuration uses Fast-CGI to pass PHP requests to PHP-FPM. If you use proxy_pass, you can use rewrite. proxy_pass is explained here.
location / {
index index.php; # Set the index file
try_files $uri $uri/ #handler; # If missing pass the URI to front handler
}
location #handler {
rewrite / /index.php;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME PATH_TO_SCRIPT$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
In my opinion, may be you should make ".htaccess file" like in Apache.