Nginx rewrite rule dynamic sub-folders and path - linux

I am trying to rewrite a URL pattern to sub-folder.
location ~^ /test/article/(.*) {
root /var/www/example.com/test;
index index.html;
try_files $uri $uri/ /test/index.html;
}
When a user requests http://www.example.com/test/article/article-name,
I need to rewrite the URL to http://www.example.com/test/, but URL should stay the same, it should display as http://www.example.com/test/article/article-name in the browser.
"article-name" could be any thing with dashes. But "article" is the hard coded path, not a real folder.
My server is currently hosting Wordress in root directory of Nginx server which is /var/www/example.com/ . I tried different things with Nginx config file and not working yet. Currently, when I typed in http://www.example.com/test/article/article-name , Wordpress will show 404 error.
Any help would be appreciated.
Thank you.
Updates:::::::
Here is my latest config file under sites-available folder. It's showing Wordpress's home page when I tried the URL.
server {
listen 8080 default_server;
listen [::]:8080 default_server;
port_in_redirect off;
set_real_ip_from 127.0.0.1/32;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
root /var/www/example.com;
index index.php index.html;
server_name example.com www.example.com;
rewrite ^/test/article/(.*) /test/;
location /test/ {
root /var/www/example.com/test/;
index index.html;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
access_log off;
expires max;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
}
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
Another update:
I am now testing with following config and it doesn't show Wordpress's 404 anymore. But it actually redirects to /test folder. I need to keep the url with "/test/article/article-name" in it.
rewrite ^/test/article/(.*) /test/;
location /test/ {
index index.html;
}

rewrite ^/test/article/(.*) /test;
location /test {
root /var/www/example.com/;
}
# create file `test` in `/var/www/example.com/`
Choice II
rewrite ^/test/article/(.*) /test/;
location /test/ {
root /var/www/example.com/test/;
index index.html;
}
# create directory `test` in `/var/www/example.com/`
# and create file `index.html` in `/var/www/example.com/test/`

Related

How to Redirect specific paths to different folder using nginx

Basically i have 3 different folders inside a main folder(example).
1 simple html page, 1 angular website, and 1 wordpress website.
lets name them
static.html
angular_website
wordpress_website
So the main domain name
www.example.com should open code from the static_html page.
The sub domain
www.example.com/site1 should direct to / open the angular_website folder
and the sub domain
www.example.com/site2 should direct to the wordpress_website folder.
following is my code.
server {
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
root /var/www/example/;
location =/ {
alias /var/www/example/
}
location =/site1 {
rewrite ^/$ http://www.example.com/site1/redirectothis/ permanent;
}
location /site1 {
alias /var/www/example/angular_website/;
try_files $uri $uri/ /index.html;
expires $expires;
}
location /site2 {
alias /var/www/example/wordpress_website/;
try_files $uri $uri/ /index.html;
expires $expires;
}
}
Please Help as i am not able to load the angular website from the path also i am doing a small redirection so that the right page is opnened for my angular website.

NGINX page redirect to homepage

I have found answers here on redirecting php and error codes, but not from one page to another. An aspect of my website is a FTP uploader and my extension is mywebsite/ss/xyz.file. Currently, I have an index.html setup in /ss so people may not see my screenshots, however I would rather just give them a 404, without making all 404s go to /ss, or automatically redirect them to my homepage.
My current serverblock config:
server {
listen 80;
listen [::]:80;
root /var/www/xxx.xxx/html;
index index.html phpinfo.php index.htm index.nginx-debian.html;
server_name mangoman.me www.xxx.xxx
location \ {
proxy_pass http://localhost:8080;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
error_page 404 /custom_404.html;
location = /custom_404.html {
root /usr/share/nginx/html;
internal;
}
}
I actually figured out my own answer.
location /ss {
return 404;
}

What does root directive in nginx refer to when it is assigned a static folder?

I have a nodejs app served with nginx for static files. On reading the sites_available of nginx, I saw root was assigned a static file serving folder (public folder to be exact). What does this mean?
My configuration looks like
server {
server_name mysite.mydomain.com;
root /var/www/mysite/public/;
index index.html index.htm;
access_log /var/log/nginx/mysite.access.log;
error_log /var/log/nginx/mysite.error.log;
underscores_in_headers on;
recursive_error_pages on;
error_page 503 #maintenance;
if (-f $document_root/system/maintenance.html) {
return 503;
}
location #maintenance {
error_page 405 = /system/maintenance.html;
rewrite ^(.*)$ /system/maintenance.html break;
}
location ~ ^/(js|css|images|media|system)/ {
autoindex off;
add_header Cache-Control public;
expires 4w;
}
location / {
try_files $uri $uri/index.html #proxy;
}
location #proxy {
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:7485;
}
}
Can any one explain this to me? I searched through the nginx documentation and didn't find any answer to my queries.
The root directive sets the base path for the requests. In other words, it is used to translate http request paths into file system paths.
For example, with your root set as /var/www/ under the server directive, if a file http://www.example.com/image.jpg is requested, it's path will be translated to /var/www/image.jpg
More details can be found here: http://nginx.org/en/docs/http/ngx_http_core_module.html#root

design files are not loading for magento in nginx server

My client moved to new server which nginx & percona server. Problem is the forward slash is being removed from end of the url. as you can see on the picture http://imgur.com/8OAUQZb
therefor the design files such as js, css files are not loading or something it gives magento 404 not found page. it happens randomly.
on the database web/unsecure/base_url and web/secure/base_url are set with forward slash http://78.137.115.47.srvlist.ukfast.net/
I'm assuming something wrong with nginx file. The rewrite rules might be wrong. I have tried every possible way that I found on this website and on google but nothing work. It might be something else. Could you please help?
This is nginx conf file for the domain
# Uncomment the server definition below should you wish to
# redirect from punkyfish.com to www.punkyfish.com
#server {
# listen 192.168.92.247;
# server_name punkyfish.com;
# #rewrite / $scheme://www.$host$request_uri permanent;
#}
#
# Change this backend name (and the socket pointer)
# as additional virtual hosts are added. This should
# point to the spawn-fcgi wrapper running as the
# appropriate user.
#
upstream punkyfishcombackend {
server unix:/var/run/php-fcgi-punkyfishcom.sock;
}
server {
listen 192.168.92.247:80;
server_name punkyfish.com;
root /var/www/vhosts/punkyfish.com/htdocs;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
location /app/ { deny all; }
location /includes/ { deny all; }
location /lib/ { deny all; }
location /media/downloadable/ { deny all; }
location /pkginfo/ { deny all; }
location /report/config.xml { deny all; }
location /var/ { deny all; }
location /. {
return 404;
}
location #handler {
rewrite / /index.php;
}
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
include "ssl_offloading.inc";
location ~ .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass punkyfishcombackend;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_param MAGE_RUN_CODE default;
# fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params;
}
This was due to nginx was caching everything. regardless of my changes on db and files wasn't reflecting. I cleaned the cache from ngnix cache folder and problem solved.
Thanks.

I can access my subdomain URL paths on both my domain and subdomain?

I have a DigitalOcean VPS running nginx which has two websites on it. The two sites on it are: www.ingledow.co.uk and blog.ingledow.co.uk.
My main (www.) domain is predominantly a static site, but my blog (blog.) subdomain runs on Ghost.
Everything works perfectly apart from that I can access my blog from both www. and blog.. For example, here is a blog post at http://blog.ingledow.co.uk/puma-social-club/, but the same blog post can be seen from http://www.ingledow.co.uk/puma-social-club/.
Another point to note is that if you try to go to http://ingledow.co.uk/puma-social-club/ without the www. or blog. it 404s.
The problem lies in having two sites on the same VPS, but not sure if there's a problem with my nginx configs or whether it's problems with my DNS, or both?
The nginx config files are in /sites-available/ and symlinked to /sites-enabled/
I need to get this fixed because it is causing issues with Google search results and SEO.
Here's my DNS:
blog.ingledow.co.uk.conf
# blog.ingledow.co.uk running on Ghost
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/ghost;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name blog.ingledow.co.uk;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:2369;
client_max_body_size 10m;
break;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /phpmyadmin { index index.php; }
}
ingledow.co.uk.conf
# ingledow.co.uk.conf
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/ingledow.co.uk/public_html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name ingledow.co.uk;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /phpmyadmin { index index.php; }
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Try adding www. to sever_name ingledow.co.uk; in the ingledow.co.uk server block. e.g. :
server_name www.ingledow.co.uk ingledow.co.uk;
If you don't want to site to be accessed without the www. subdomain prefix you should remove it from the server_name.
Another way to do it is to have the server block as is for the blog, and just use a catch all server block for the main static site.

Resources