nginx dynamic change root of subdomain to folder on same level - linux

I have this hierarchy on my server:
/var/www/domain.tld/web
/sub
The root in the /etc/nginx/sites-available/domain.tld is set like this:
server {
root /var/www/domain.tld/web;
index index.html index.php index.htm;
...
location / {
try_files $uri $uri/ =404;
}
How can I make the nginx to dynamically forward to specific folder so if I make folder in /sub for example /var/www/domain.tld/sub/subdomain so if I make request http://subdomain.domain.tld it will forward to this folder in folder sub?
So again something.domain.tld would be going to /var/www/domain.tld/sub/something but domain.tld would be going to /var/www/domain.tld/web
Thank you!

First off, welcome to StackOverflow! :)
There are probably a few ways of achieving this, but I would try a simple map to set a $my_subdir variable from the $host, which you can then use in your root. The $host variable is set automatically from the incoming request.
In this example, we're using a regular expression to check if the $host matches subdomain.domain.tld. If it does, capture the subdomain part - the bit in brackets - and use that to make the value "sub/subdomain" ($1 means use the first thing captured from the regular expression). If it doesn't match, it defaults to "web".
That way,
when the request comes in as something like http://domain.tld/foo.html, the response served is /var/www/domain.tld/web/foo.html
if the request comes in as something like http://bar.domain.tld/foo.html, the response served is /var/www/domain.tld/sub/bar/foo.html
map $host $my_subdir {
~^(?:www\.)?((?!www\.)[^.]+)\.domain\.tld$ sub/$1;
default web;
}
server {
root /var/www/domain.tld/$my_subdir;
index index.html index.php index.htm;
...
location / {
try_files $uri $uri/ =404;
}

Related

Htaccess and nginx converting

I'm converting this htaccess file htaccess file
to nginx using
http://winginx.com/ru/htaccess
but don't understand where i should paste result. I have created
include file
include /etc/nginx/myfile
and pasted file there but when i'm reloading (restarting) Nginx it Fails.
Could you help me?
I suspect these apache .htaccess converters do not fully utilize the unique features of nginx. I would recommend trying something like this:
server {
listen 80 default_server;
server_name _;
root /var/www/example.com;
index index.php;
# if the file or directory doesn't exist, serve /index.php
try_files $uri $uri/ /index.php;
# if the request is exactly /sitemax.xml, serve sitemap_xml.php
location = /sitemax.xml {
try_files /modules/sitemap/sitemap_xml.php =404;
}
# hide regex location in a prefix location to avoid confusion
# introduced by multiple regex locations
location /pages-print {
location ~ ^/pages-print(\d+) {
try_files /modules/pages/print.php?page=$1 =404;
}
}
}
I have found solution. I tried paste result from the convertor to nginx.conf and it was mistake. I, instead created file in /etc/nginx/ and named it htac.rules (i think it doesn't matter of how you'll name it).
Then i opened file etc/nginx/sites-enabled/default and in location / inserted include /etc/nginx/htac.rules
so.
in htac.rules i inserted converting result without location / {...}
and, yeah. It solved my problem.
Thanks everyone for helping me.

Nginx rewrite non-existent files under sub directory with rewrite rules

Nginx rewrite from .php to .php under same location
I am trying to move a site with a large number (93) of .htaccess files from Apache to Nginx.
There are over 800 rewrite rules to convert. To optimize performance, I have decided to
place the rules under separate nested location blocks (61 blocks in total).
But I am having problem with rewriting some of the rules.
Requirements:
/test/abc/pics/ should redirect to /test/abc/wallpapers/
/test/abc/pics/800/a.jpg should be served directly, if the file is present. If it does not exist,
then the request should be rewritten to /test/abc/pics/pic.php, which will
create the file on first request. Subsequent requests will be served directly.
/test/abc should redirect to /test/abc.html
/test/abc.html should be rewritten to /test/index.php
/test/abc/def/year-2014.php should be rewritten to /test/abc/def/index.php
All static files (.gif, .jpg, .png, .mpg....) should be served directly with a very long expiry time.
All files ending in .html should be served directly. If the file does not exist the request should be rewritten to /fallback.php
All files ending in .php or / should be executed by PHP-FPM. If the file does not exist it should be rewritten to /fallback.php
Config:
index index.php;
#
# Location Specific Rules
#
location /test/abc/ {
location /test/abc/pics/ {
# For ensuring 'serve directly, if file exists' condition of Rule 2
try_files $uri #rewrite_pics;
}
location /test/abc/def/ {
try_files $uri $uri/index.php #rewrite_def;
}
}
location /test/ {
rewrite "^/test/([a-z-]+)$" /test/$1.html permanent; # Rule 3
rewrite "^/test/([a-z-]+).html$" /test/index.php?symbol=$1 last; # Rule 4
}
location #rewrite_pics {
rewrite "^/test/abc/pics/$" /test/abc/wallpapers/ permanent; # Rule 1
rewrite "^/test/abc/pics/([0-9]+)/(.*)$" /test/abc/pics/pic.php?width=$1&height=$1&pic=$2 last; # Rule 2
}
location #rewrite_def {
rewrite "(?i)^/test/abc/def/year-([a-z]+).php$" /test/abc/def/index.php?year=$1 last;
}
#
# Fallback Rules
#
location ~* \.(gif|jpg|jpeg|png|ico|3gp|flv|mpg|mpeg|mp4|mp3|swf|txt|xml|pdf|zip)$ {
expires 1M; # Rule 5
try_files $uri /404.php;
}
location ~ (\.php|\.html|/)$ {
try_files $uri "${uri}index.php" /fallback.php =404;
include php-fpm.conf;
}
php-fpm.conf
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
Problem:
Since the fallback rules are regular expressions, they are selected instead of the location specific rules
If I use ~ prefix for all location specific rules, then the fallback rules are never executed.
Request for /test/abc/pics/800/a.jpg will be rewritten to /test/abc/pics/pic.php which will again match the
same location config. So the file is not executed and is downloaded as is.
So, I added 'include php-fpm.conf;' to all location blocks (except named locations). But this causes all
requests under /test/abc/pics/ (including existing .jpg files) to pass to the PHP-FPM. Which leads to an access denied error,
since only .php extension is allowed by security.limit_extensions.
What am I doing wrong here? Is there a simple way to have all existing files served based on their extension
and only non existent files match the location blocks / rewrite rules?

Nginx redirect all media (.mp3 , image , pdf) to MaxCDN origin poll

I want to redirect all my media files to maxcdn origin poll, this might be duplicate question
but couldn't find the way i was looking for.
example:
If visitor request something like : http://domain.com/monday/day1.mp3 it needs to redirect to http://xxx.netdna.com/monday/day1.mp3.
Question: --- No To This :/
Will nginx allow .htaccess to do this job?
Or
Do I need to setup server config? How
Here is my config, which is every simple.
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/domain/public_html;
index index.html index.php index.htm;
# Make site accessible from http://localhost/
server_name domain.com;
return 301 http://XXX.YYYY.netdna-cdn.com$request_uri;
The page isn't redirecting properly
Please Help!
Will nginx allow .htaccess to do this job?
No. There is no .htaccess analogue for nginx.
This will redirect all request to folder /podcast/ to xxx.netdna.com.
location /podcast/ {
return 301 http://xxx.netdna.com$request_uri;
}
I have found solution for my problem after a huge dig search on google, but the answer was so simple, didn't realized till it turned on working perfectly for my problem... :)
All I need to add was, following code right after location /
Here is what i added and worked like charm.
try_files $uri $uri/ /index.php;

Nginx rewrite rules with try_files

I need help to converting this .htaccess to Nginx. Here is the .htaccess code:
RewriteRule wp-content/thesis/skins/(.*)/css.css wp-admin/admin-post.php?action=thesis_do_css
I already convert those rules to online nginx converter. So far I get this code:
rewrite /wp-content/thesis/skins/(.*)/css.css /wp-admin/admin-post.php?action=thesis_do_css;
I put those code above under try_files rules. Here is the complete code:
location / {
try_files $uri $uri/ /index.php?q=$uri$is_args&$args;
rewrite /wp-content/thesis/skins/(.*)/css.css /wp-admin/admin-post.php?action=thesis_do_css;
}
So, it doesn't work. Please help me understanding about nginx rules. Thanks.
Beny
try_files is final. You cannot put anything after it and expect it to be evaluated. Think of try files as an if-then-else statement, where the last part is always the else. Since there are probably a lot more rules involved, you are best off consulting the WordPress codex.
If you need just to rewrite that one css.css file to wp-admin/admin-post.php?action=thesis_do_css then try this:
if ($request_filename ~ wp-content/thesis/skins/classic-r/css.css ) {
rewrite ^ http://mydomain.com/wp-admin/admin-post.php?action=thesis_do_css? permanent;
}
That should be within your server{ block. Make sure to change mydomain.com to your actual domain, of course.
I hope that helps.

How to remove the index.php in the url?

my host server is nginx.
in the root directory i have installed magento, then i created a file named blog, then installed wordpress into it.
now, the directory is:
app
includes
....
blog/wp-admin
when i set http://www.example.com/blog/index.php/%postname%.html in Permalink Settings. access all the post, it's ok. now i want to remove the index.php. namely change it into http://www.example.com/blog/%postname%.html.when i delete the index.php. all the post are not unavailable,
EDIT:
i put the code in the nginx.conf.
location /blog/ {
index index.php;
try_files $uri $uri/ /blog/index.php?$uri&$args;
rewrite ^ /index.php? last;
}
but it doesn't work. is there something i lost?

Resources