Nginx Redirects Subfolders - nginx

I have on my server installation of WordPress in public directory:
var/www/example/ - main installation of WordPress
In that directory I have also subdirectory WORDPRESS which has many subfolders like
var/www/example/WORDPRESS/1
var/www/example/WORDPRESS/2
etc.
Every this subfolders 1,2 have installed WordPress Blog. Everythings works ok when I use standard permalinks for wordpress for example:
http://www.example.net/WORDPRESS/1/?p=1
But when I try to change it to somethink like:
http://www.example.net/WORDPRESS/2015/09/27/title-of-post
Nginx redirect to:
http://www.example.net/2015/09/27/title-of-post/
My nginx configuration:
server {
listen 80;
server_name example.net *.example.net;
rewrite ^/(.*)$ http://www.example.net/$1 permanent;
}
server {
listen 80;
server_name www.example.net;
root /var/www/example/;
index index.php index.html index.htm;
client_max_body_size 128M;
location / {
root /var/www/example/;
index index.php index.html index.htm;
try_files $uri $uri/ /index
}
How can I make subfolders work? Nginx just skips WORDPRESS subdirectory in link.

I think you can try with:
location /WORDPRESS/ {
root /var/www/example/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}

Related

Reverse proxy nginx to itself

I am currently hosting a single-page react app that is hosted in the URL root like so:
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I need to put the site behind an AWS elastic load balancer and at the same time change the path so everything is within a /support directory e.g. http://example.com/index.html -> http://example.com/support/index.html.
AWS ALBs do not support URL rewriting so I have to do this within the nginx config on the server. First of all I tried changing the config to:
server {
listen 80;
server_name localhost;
location /support {
alias /var/www/html;
try_files $uri /index.html;
}
}
This sort-of works but the URLs within the javascript content don't contain the /support path (e.g. they contain http://example.com/script.js instead of http://example.com/support/script.js).
I then tried creating a reverse-proxy config to proxy /support to /, which sadly put nginx in an infinite loop until it ran out of worker threads:
server {
listen 80;
server_name localhost;
location /support {
proxy_pass http://localhost:80;
}
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I'm confused why requests are going into a reverse-proxy loop? Shouldn't proxy_pass remove the /support prefix before proxying the request, and therefore it shouldn't be "caught" again by the /support location?
Just a guess.
Do you want to serve something on /?
If not - it is easy:
server
{
listen 80;
server_name localhost;
location /support/
{
alias /var/www/html/;
try_files $uri $uri/ /index.html;
}
location /
{
return 303 http://localhost/support$request_uri;
}
}
Fiddle around with the ending slashes if it does not work (using them - or not - makes often a difference).
Use alias instead of root so that /support is not added to the /var/www/html folder.
Everything gets redirected to /support.
If you want to serve something on / which is different from /support:
Use sub_filter or subs_filter in /support to rewrite your source code links on-the-fly so that they will never use /.
If you have redirects inside your source code (or proxy_pass backend) - you need proxy_redirect and/or Lua to catch and change them on-the-fly.

Nginx configuration for displaying contents in home directory

The requirement is to display contents in local user's home directory when you enter /~user.
www.blahblahblah.com/~user
I've created a local user with home directory and configured Nginx like that:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm test.html;
# Make site accessible from http://localhost/
server_name localhost;
location ~* /\~(.*)/$ {
root /home/$1/;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
But I still got 404 not found error.
The problem is that the root and the URI are concatenated to obtain the request filename, so you are asking nginx to deliver root /home/user/~user/.
The solution is to use a rewrite ... break to sanitise the URI. You can extend the regular expression to capture both elements and then use the named captures in a root and a rewrite ... break statement:
location ~* /\~(?<username>\w+)(?<userpath>/.*)$ {
root /home/$username;
rewrite ^ $userpath break;
}

nginx Redirect subfolder to root domain

I want to redirect the subfolder and all contents to root domain.
For example:
http://www.example.com/ubb/ will redirect to http://www.example.com
My server configuration is like below:
server {
listen 80 default_server;
root /home/vishant/devcenter/wava-v1.1/HTML;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name baetter.l;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#proxy_pass "http://127.0.0.1:3000";
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
i have found similar problem solved using htaccess here
But how can i achieve in nginx??
One of a number of solutions is:
location ^~ /ubb/ {
return 302 /;
}
The ^~ modifier ensures that this prefix location continues to take precedence if you were to add any regex locations in the future. See this document for details.
The return directive is documented here.

NGINX alias with HHVM

I'm trying to reroute one of my web directories to another project directory in the home folder. But when I try and access the page, I get served a blank file
I believe the fault in on the try_files $uri $uri/ /rtstaging/index.php?$query_string; line. But I'm new to using nginx, so any help would be much appreciated
server {
listen 80;
listen [::]:80;
root /home/default/public;
index index.php index.html index.htm;
server_name nu-voo.co.uk www.nu-voo.co.uk;
include hhvm.conf;
location /rtstaging/ {
alias /home/rtsearch/public/;
try_files $uri $uri/ /rtstaging/index.php?$query_string;
include hhvm.conf;
}
}

How can I mask a Nginx virtual host redirect?

I was asked to make a rewrite rule to redirect a virtual host subdomain to another virtual host managed by the same Nginx server. That works fine but I'd like to mask the URL redirect in the browser.
There is a configuration file for each virtual host: subdomain.mydomain.com.conf configuration file was made from original www.mydomain.com.conf with required substitutions.
This is www.mydomain.com.conf virtual host configuration file:
server {
listen 127.0.0.1:80;
listen 443 ssl;
server_name www.mydomain.com;
access_log /var/log/nginx/www.mydomain.com-access_log main;
error_log /var/log/nginx/www.mydomain.com-error_log;
root /var/www/html/www.mydomain.com;
index index.php index.html index.htm;
...
location / {
try_files $uri $uri/ /index.php?$args;
}
...
I made a Nginx rewrite rule to redirect URLs beginning with www.mydomain.com/subdomain to subdomain.mydomain.com:
location /subdomain {
rewrite ^/$ subdomain.mydomain.com$request_uri last;
}
I found a link that explains how to make an internal redirect :
http://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.VFKqiYXXqPK
I made several attempts but the redirect is always shown.
As I'm new to Nginx, can somebody help me out ?
best regards,

Resources