Exception for Nginx Rewrite - nginx

I rewrite all requests to an index.php file in Nginx. If i try now to use my AWStats folder i can't see any icons or images because of the rewrite.
if (!-e $request_filename){
rewrite ^\/(.+)$ /index.php?code=$1 last;
}
location /stats/ {
index index.html index.php;
auth_basic "Members Only";
auth_basic_user_file /var/www/clients/client1/web6/web//stats/.htpasswd_stats;
add_header Content-Security-Policy "default-src * 'self' 'unsafe-inline' 'unsafe-eval' data:;";
}
location ^~ /awstats-icon {
alias /usr/share/awstats/icon;
}
}
So normally i would wrap it in a location like
location /{
if (!-e $request_filename){
rewrite ^\/(.+)$ /index.php?altum=$1 last;
}
Followed by
location /stats/ {
# Example
}
But this is for some reason not working, Nginx would not start anymore after this. I hope someone could see my mistake.

Related

Nginx mp4 hotlink protection doesnt work

I am trying to block hotlinking of my mp4 with nginx "valid_referers" :
valid_referers none blocked mysite.com;
if ($invalid_referer) {
return 403;
}
But it doesnt work at all, the mp4 still display on the websites who are stealing the video and if I put some random domain instead of "mysite.com" the video still work.
If it can help, this is what my server conf file look like :
server {
listen 80;
server_name dl.mysite.com;
root /home/videos;
index index.php index.html;
autoindex off;
location ~ /\. {
deny all;
}
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
# streaming
location ~ \.mp4$ {
valid_referers none blocked mysite.com;
if ($invalid_referer) {
return 403;
}
gzip off;
gzip_static off;
mp4;
mp4_buffer_size 1M;
mp4_max_buffer_size 300M;
}
location ~ .flv$ {
flv;
}
# removes trailing slashes
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# canonicalize codeigniter url end points
if ($request_uri ~* ^(/lobby(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# catch all
error_page 404 /index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_read_timeout 600;
include fastcgi_params;
}
}
I am doing something wrong ?
I had the same problem in past. I thought it was not working, but actually, it was working. Just make sure you are not allowing anyone except you.
Sometimes leechers try null or empty referrers to bypass this and there is no possible solution to this. One possible solution I can suggest to block the resource directory. See https://www.atulhost.com/hotlink-protection-nginx this man explained everything in a simple way.

Access index.html in folder without 301 redirect

I have some index.html files sitting in a folder to get some nice urls -
site.com/about
where index.html sits in the about folder. But I am seeing that my site.com/about is being 301 redirected to site.com/about/ I am not sure where the 301 is generated from. It is not in config.
/about/ also has a 301 result.
I guess it makes sense since I am redirecting to the index.html file but should it not be a rewrite? Is there a way to return 200 for /about instead of 301 to about/?
I am using nginx
Server Block:
server {
listen IP;
server_name site.com;
rewrite / $scheme://www.$host$request_uri permanent;
}
server {
listen IP:80;
server_name site.com *.site.com;
root /var/www/vhosts/site.com/htdocs;
charset utf-8;
rewrite_log on;
location / {
index index.html index.php;
try_files $uri $uri/ /$uri.php;
expires 30d;
}
if ($request_uri = /index.php) {
return 301 $scheme://$host;
}
if ($request_uri = /index) {
return 301 $scheme://$host;
}
location /. {
return 404;
}
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
include "ssl_offloading.inc";
location ~ .php$ {
# if (!-e $request_filename) { rewrite / /index.php last; }
if (!-e $request_filename) { rewrite / /404.php last; }
}
}
The index directive and the $uri/ element of the try_files directive, has the side-effect of adding a trailing / to directory names by performing an external redirect.
To avoid the external redirect and return an appropriate index file when presented with a slash-less directory name, implement the index functionality explicitly within the try_files directive:
location / {
try_files $uri $uri/index.html $uri.php;
expires 30d;
}
Notice that .php works only in the last element at this location. If you need to check for $uri/index.php (in addition to $uri.php) you can use a named location block - and move or copy your fastcgi configuration into it.
For example (based on your server block):
root /var/www/vhosts/site.com/htdocs;
error_page 404 /404.php;
location / {
try_files $uri $uri/index.html #php;
expires 30d;
}
location #php {
try_files $uri.php $uri/index.php =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
...
fastcgi_pass ...;
}
location = /index.php { return 301 $scheme://$host; }
location = /index { return 301 $scheme://$host; }
location /. { return 404; }
location ~* \.php(/|$) { rewrite ^(.*)\.php $1 last; }
include "ssl_offloading.inc";

Nginx auth_basic and rewrite in subdirectory does not work

I'm trying to add a project to a subfolder of existing webserver with Nginx. Here's my simple config:
server {
listen 80 default_server;
server_name localhost;
root /var/www;
[...]
location = /my-project { return 301 /my-project/; }
location /my-project/ {
alias /var/www/my-project/web/;
index index.php;
location ~ /[^/]+/control(/|$) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
if (-f $request_filename) { break; }
rewrite ^(.*)$ /my-project/index.php last;
}
if (-f $request_filename) { break; }
rewrite ^ /my-project/index.php last;
location ~ ^/[^/]+/index\.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgi.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
}
Because there is an rewrite directive inside /control location block, the auth_basic never gets triggered, because Nginx executes rewrites before authentication. In which way should I modify the config, that auth does work?
PS: try_files doesn't seem to work, because it serves files from root (/) webfolder!? When I replace the if and following rewrite with try_files $uri /my-project/index.php?$query_string; I get a 404, because Nginx tries to serve the file /var/wwwindex.php (have a look at missing slash and the root folder /var/www instead of alias).
EDIT 18.09.2013:
As VBart suggests I'm using now the following configuration to get authentication to work
location ~ ^/(?<dir>my-project)(?<path>/.*)$ {
root /var/www/$dir/web;
location ~ ^/[^/]+/control(/|$) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
try_files $path /$dir/index.php?$query_string;
}
try_files $path /$dir/index.php?$query_string;
location ~ ^/[^/]+/index\.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgi.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
Use:
error_page 404 =200 /my-project/index.php;
instead of ugly rewrites:
if (-f $request_filename) { break; }
rewrite ^(.*)$ /my-project/index.php last;
Reference:
http://nginx.org/r/error_page
http://wiki.nginx.org/IfIsEvil
http://wiki.nginx.org/Pitfalls
P.S. try_files doesn't work with alias because of bug: http://trac.nginx.org/nginx/ticket/97, but you can replace alias with root directive: http://nginx.org/r/root

config nginx to access index and spec_runner pages

index.html page could access, but not spec_runner.html.I want to access two different pages, one for app, another for tests. Could you help me? thanks, :).My configure is following
location ^~ /p/login { rewrite .* /index.html last; }
location ^~ /specs { index spec_runner.html; }
location ~* ^/(users|books) {
proxy_pass http://api;
}
location / {
root /home/user/project;
proxy_cache off;
expires -1;
add_header Cache-Control no-cache;
add_header Cache-Control private;
}

wordpress site in subdomain on nginx

I'm having trouble setting up a subdomain with nginx and some wordpress sites.
My www.jackalopegames.com domain is working, but I want to set up dev.jackalopegames.com.
Here's the config file in my sites-enabled folder:
server
{
listen 80;
server_name jackalopegames.com www.jackalopegames.com;
include /etc/nginx/fastcgi_php;
root /var/sitefolder;
index index.php;
}
server {
listen 80;
server_name dev.jackalopegames.com;
include /etc/nginx/fastcgi_php;
root /var/devfolder;
index index.php;
}
The first one server_name jackalopegames.com works, but the second one doesn't. I've looked around a bunch and I'm at a loss as to why this isn't working. Any tips would be appreciated!
Update:
I've added the following to my subdomain server {...} with no effect:
location / {
root /var/dev;
index index.php;
rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
if (!-e $request_filename) {
rewrite ^.+/?(/wp-.*) $1 last;
rewrite ^.+/?(/.*\.php)$ $1 last;
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{
root /var/dev;
rewrite ^/.*(/wp-.*/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$/wp-includes/ms-files.php?file=$1 last;
expires 30d;
break;
}
location ~ wp\-.*\.php|wp\-admin|\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/dev$fastcgi_script_name;
}
}
There are a bunch of things that you may or may not have already checked. I had similar issues myself when I first did this. So, to help with just thing thing, I wrote up a walkthrough for how to do it. You can find it here: http://blog.phpadvocate.com/2014/10/setting-up-your-wordpress-blog-as-a-subdomain-with-nginx/

Resources