in nginx return source as text - mod-rewrite

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.

Related

MVC inside Drupal 7 nginx url rewrite

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;
}

nginx rewrite rule conflict

I am having a conflict with two blocks with nginx 1.8.0.
The first block is to setup static cache for certain file types:
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|swf)$ {
add_header "Access-Control-Allow-Origin" "*";
access_log off;
log_not_found off;
expires max;
}
The second block is a series of rewrites defined by filetype:
location /files {
rewrite ^/files/master\.([0-9]+)?\.css$ /min/?g=css&456 break;
rewrite ^/files/master\.([0-9]+)?\.js$ /min/?g=js&456 break;
rewrite ^/files/second\.([0-9]+)?\.js$ /min/?g=jsa&456 break;
}
The rewrites result in a 404. Any rewrite that uses a filetype defined in the static cache rule results in a 404 error. If I change the rewrite rule to a different filetype or comment out the static cache file block, it works.
What am I missing in the cache static files that is preventing a rewrite from being performed at a later config setting?
After much gnashing of teeth, I ended up changing the redirects to a try_files parameter. The parameters must be higher in the conf file than the static cache file.
location ~ ^/files/master\.([0-9]+)?\.css$ {
try_files $uri /min/?g=css&456;
}
location ~ ^/files/master\.([0-9]+)?\.js$ {
try_files $uri /min/?g=js&456;
}
location ~ ^/files/second\.([0-9]+)?\.js$ {
try_files $uri /min/?g=jsa&456;
}
This will allow me to run the minify toolset.

passenger: pass request to rack application if no file found

If it were php, it'd be something along the lines:
server {
server_name ...;
root /path/to/root/1;
location / {
try_files $uri #root2;
}
location #root2 {
root /path/to/root/1;
try_files $uri /index.php?$args;
}
location = /index.php {
include fastcgi_params;
fastcgi_pass php;
}
}
In other words, the point is to separate public files into several directories. Is there a way to do this with passenger + nginx + sinatra (to be precise)?
UPD Basic nginx/passenger setup:
server {
server_name example.com;
root /home/yuri/example.com/public;
passenger_enabled on;
}
For more information see documentation.
You could try something like this :
server {
[...]
root /path/to/public/folder;
[...]
location / {
# Serve static files or forward to passenger
try_files $uri $uri.html #passenger
}
location #passenger {
passenger_enabled on;
...
}
}

Block direct access to php files (only rewrited) using nginx

I have files like Ucp.php Somesite.php and i want to create a rewrite rules for nginx. This is not problematic.
But i also want to block direct access to those php files, so that enetring http://mysite.com/Ucp.php will return 404 Not Found.
There is a solution for apache (using THE_REQUEST), but how can I do this on nginx?
server
{
listen 80;
server_name site.com;
root /home/site/public_html;
index Main.php;
rewrite ^/SomeAddr/$ /SomeAddr.php last;
rewrite ^/SomeOtherAddr/$ /SomeOtherAddr.php last;
location ~ \.php$
{
fastcgi_pass unix:/var/run/php-fpm/site_com.sock;
include fastcgi.conf;
}
include security.conf;
}
You can use internal directive for this purpose.
location ~ \.php$
{
internal;
fastcgi_pass unix:/var/run/php-fpm/site_com.sock;
include fastcgi.conf;
}
location ~ \.php$ {
return 404;
}

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