MVC inside Drupal 7 nginx url rewrite - model-view-controller

Senario
I have a Drupal 7 installation with nginx configuration like this
And i want a custom MVC app to be run on a subfolder named mvcp
for that i use this
location /mvcp {
root /var/www/html/site/mvcp;
index index.php;
try_files $uri $uri/ index.php?$args;
}
on curl -I url i get this
HTTP/1.1 404 Not Found
Server: nginx
Date: Tue, 24 Nov 2015 05:40:29 GMT
Content-Type: text/html
Connection: keep-alive
EDIT
modified to
location /mvcp {
try_files $uri $uri/ /mvcp/index.php?q=$uri&$args break;
}
now main /mvcp/ works but not the sub paths..

In your original post, the location block looks in /var/www/html/site/mvcp/mvcp because the root and URI are concatenated to form the local path.
In your EDIT, try_files is incorrect as the last element should be a =code or URI. See this document. Remove the break element, try:
location /mvcp {
try_files $uri $uri/ /mvcp/index.php?q=$uri&$args;
}

Related

Deploying Vue Cli 3 SPA with Laravel backend

I have a Vue Cli 3 SPA application which makes api calls to a Laravel Backend. I've created a LEMP droplet on DigitalOcean, and I've cloned the two projects in the /var/www/html directory. api/ for the backend, web/ for the frontend. I've configured nginx root to web/dist/index.html. Now, how can I make api calls, since the root of the project is index.html?
I've searched a lot. I saw solutions where I must copy the dist folder's contents to api/public, and adjust nginx's root to be api/public/index.html. But that doesn't change the fact that I still can't make api calls, because index.php is never reached.
Could you please advice me how you do it? Should I create a subdomain?
Thanks!
UPDATE
I've tried this according to oshell's answer:
# For the vue app
server {
listen 80;
root /var/www/html/web/dist;
index index.html;
server_name XXX.XXX.XX.XXX # the ip addreess that I have
error_page 404 /;
location / {
try_files $uri $uri/ /index.html;
}
}
# for the laravel application
server {
listen 80;
root /var/www/html/api/public;
index index.php;
server_name XXX.XXX.XX.XXX/api;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Now whatever I open, it just goes to the vue application. If I try to make an api call to XXX.XXX.XX.XXX/api/something from the vue app, I've got 405 Method not allowed
You need to setup two separated servers for frontend and backend. You could make api reachable via api.example.com and frontend via example.com. The nginx config should look something like this:
#laravel.conf
server {
listen 80;
root /var/www/html/project_name/api;
index index.php index.html index.htm;
server_name api.example.com www.api.example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
#vue.conf
server {
listen 80;
root /var/www/html/project_name/web/dist;
index index.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.html;
}
}
You could also direct all traffic to you index.php and set it up so Route::any('/') returns the static page, including the static assets and all api routes are handled via Route::any('/api/foo').
The following configuration works for me on local environment - home directory on ubuntu.
Folder structure
example/dist - vue application
example/laravel - laravel api application
example/laravel/public - laravel public directory
example/laravel/public/images - laravel api images directory
Urls
example.lo - vue application
example.lo/api - laravel api
server {
# server name and logs
server_name example.lo;
access_log /var/log/nginx/example.lo_access.log;
error_log /var/log/nginx/example.lo_error.log;
root /home/username/example/laravel/public/;
index index.html index.php;
# location for vue app
location / {
root /home/username/example/dist/;
try_files $uri $uri/ /index.html;
}
# location for laravel api
location /api {
try_files $uri $uri/ /index.php$is_args$args;
}
# location for api images
location /images {
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}

How to rewrite url with try_files css, js file [ laravel + vue-cli nginx]

I'm using vue-cli for frontend and Laravel for backend
Here is my Nginx config:-
server{
// Here I set up laravel in root project
listen 80;
root /var/www/html/serverside/public;
index index.php index.html index.htm;
server_name localhost;
//Here is vuejs project
location / {
root /var/www/html/frontend;
try_files $uri $uri/ = /index.html;
}
//Here is example when I want to use route path in laravel project
location /admin {
try_files $uri $uri/ /index.php?$query_string;
}
}
The Problem is assets() in Laravel redirect to /var/www/html/frontend;
How can I make asset in Laravel redirect in /var/www/html/serverside/public;
I try to do something like this:-
// if enter /admin-js I want to read app.js file but it didnt working
location /admin-js {
rewrite ^/admin-js/$ /var/www/html/serverside/public/js/app.js;
try_files $uri $uri/ /index.php?query_string;
}

Add slash and cut extension of file (nginx rules)

I tried to figure out how to add "/" to the end of every url.
And this url need to be without extension.
For example:
example.com/about.php; example.com/about.html => example.com/about/
I'm dummy in configuration nginx rewrite rules.
This is my config:
server{
root /usr/share/nginx/www/example.com;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name example.com www.example.com;
client_max_body_size 20M;
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css text/x-component application/x-javascript application/json application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text$;
# END W3TC Browser Cache
location /blog {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
# try_files $uri $uri/ /index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~* ^.+\.(rss|atom|jpg|jpeg|gif|png|ico|rtf|js|css|json)$ {
expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.php index.html index.htm;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
I founded some solutions but i can't to combine it, because I'm noob in nginx.
You need to change part of your config like that
location / {
rewrite ^(/.*[^/])\.(html|php)$ $1/ permanent;
rewrite ^(/.*[^/])(?!/)$ $1/ permanent;
try_files $uri $uri.html $uri/ #extensionless-php;
index index.php;
}
location #extensionless-php {
rewrite ^(.*)/$ $1.php last;
}
First line rewrite ^(/.*[^/])\.(html|php)$ $1/ permanent; will redirect all requests with extensions .html or .php
rewrite ^(/.*[^/])(?!/)$ $1/ permanent;
Will add / to requests, which uri end without /

in nginx return source as text

I wish to be able to return filename.jade and filename.styl as text mime type.
nginx needs to serve anything in the /source directory as if it were text
right now using symlinks
/source/subdir/page1.jade.txt => ../subddir/page1.jade
and in nginx
location ~ ^/source/ {
expires 1d;
try_files $uri.txt 404;
}
this works, but not very elegant, and the symlinks need to be recreated
how is this accomplished purely in nginx? is it necessary to use rewrite to chop off the /source directory from the path?
It will be better for you to use specific location directive with types directive as example belove:
location ~ /*.jade {
types { }
default_type text/plain;
try_files $uri =404;
}
location ~ /*.styl {
types { }
default_type text/plain;
try_files $uri =404;
}
Here for both location blocks you set to do not use any MIME-Types and to use as default text/plain MIME-Type so it should work for you.

nginx redirect all directories except one

I'm using nginx 1.0.8 and I'm trying to redirect all visitors from www.mysite.com/dir to google search page http://www.google.com/search?q=dir where dir is a variable, however if dir=="blog"( www.mysite.com/blog) I just want to load the blog content(Wordpress).
Here is my config :
location / {
root html;
index index.html index.htm index.php;
}
location /blog {
root html;
index index.php;
try_files $uri $uri/ /blog/index.php;
}
location ~ ^/(.*)$ {
root html;
rewrite ^/(.*) http://www.google.com/search?q=$1 permanent;
}
if I do this even www.mysite.com/blog will be redirected to google search page. If I delete the last location www.mysite.com/blog works great.
From what I've read here: http://wiki.nginx.org/HttpCoreModule#location it seems that the priority will be first on regular expressions and that first regular expression that matches the query will stop the search.
Thanks
location / {
rewrite ^/(.*)$ http://www.google.com/search?q=$1 permanent;
}
location /blog {
root html;
index index.php;
try_files $uri $uri/ /blog/index.php;
}
http://nginx.org/r/location
http://nginx.org/en/docs/http/request_processing.html
This situation can also be handled using only regex. Though this is a very old question and it has been marked answered, I'm adding another solution.
If you use multiple loop forwards using reverse proxy this is the easiest way without having to add a separate location block for every directory.
root html;
index index.php;
location / { #Match all dir
try_files $uri $uri/ $uri/index.php;
}
location ~ /(?!blog|item2)(.*)$ { #Match all dir except those dir items skipped
rewrite ^/(.*) http://www.google.com/search?q=$1 permanent;
}

Resources