Nginx URL wise redirect - nginx

I have a some case to map in nginx
for / it take from the /var/www/html/cont
for /content/* it take form the var/www/html/cont
if it is not / and not /content/*
the it should take from /var/www/html/web
i am stucking in case 3
Here is my config
location / {
root /var/www/html/cont;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
# try_files $uri $uri/ =404;
}
location /content {
root /var/www/html/cont;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
# try_files $uri $uri/ =404;
}
Any help appreciated.
Thank you

Set root for server {} context as root /var/www/html/web;
then
location / {
root /var/www/html/cont;
# your other directives
}
location /content {
root /var/www/html/cont;
# your other directives
}

Here is my solution which works perfectly in as per my use case.
location / {
if ($request_uri = "/") {
set $custom_root /var/www/html/cont;
}
if ($request_uri != "/") {
set $custom_root /var/www/html/web;
}
root $custom_root;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html break;
}
# try_files $uri $uri/ =404;
}
location /content {
root /var/www/html/cont;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html break;
}
}

Related

Nginx rewrite gives 404

On a server with Ubuntu 20.04 and nginx 1.18.0 using ISPConfig 3.2 I have the following configuration for my Wordpress website:
location /api {
rewrite ^(.*)$ $1.php last;
}
location ~* ^/index\.php$ {
break;
}
try_files $uri $uri/ /index.php$is_args$args;
location /user {
try_files $uri $uri/ /index.php$is_args$args;
rewrite ^/user/([^/]+)/([^/]+)/master?$ /index.php/master/?userid=$2 redirect;
rewrite ^/user/([^/]+)/([^/]+)/chief?$ /index.php/chief/?userid=$2 redirect;
rewrite ^/user/([^/]+)/([^/]+)?$ /user/?userid=$2 redirect;
}
The redirect for e.g. /user/john/123 to /user/?userid=123 is working as expected.
But I do not want a redirect but to keep the URL as it is and show the content of the internally rewritten URL. So I tried to change redirect to break or last but both changes are resulting in a 404.
...
location /user {
try_files $uri $uri/ /index.php$is_args$args;
rewrite ^/user/([^/]+)/([^/]+)/master?$ /index.php/master/?userid=$2 last;
rewrite ^/user/([^/]+)/([^/]+)/chief?$ /index.php/chief/?userid=$2 last;
rewrite ^/user/([^/]+)/([^/]+)?$ /user/?userid=$2 last;
}
Why is it working with the redirect but not without?
Edit
Another try:
location /api {
rewrite ^(.*)$ $1.php last;
}
location ~* ^/index\.php$ {
break;
}
try_files $uri $uri/ /index.php$is_args$args;
location /user {
try_files $uri $uri/ #userrules;
}
location #userrules {
rewrite ^/user/([^/]+)/([^/]+)/master?$ /index.php/master/?userid=$2 redirect;
rewrite ^/user/([^/]+)/([^/]+)/chief?$ /index.php/chief/?userid=$2 redirect;
rewrite ^/user/([^/]+)/([^/]+)?$ /index.php/user/?userid=$2 redirect;
rewrite ^/(.*)$ /index.php?$args last;
}
Same result - if I remove the redirect it results in a 404.
The resulting page e.g. /user/?userid=203 works but I would like to keep the readable URLs.

Nginx doesnt work on my CloudPanel Website

Open your server's root nginx.conf file, most of the time It's located it in: /etc/nginx/nginx.conf
Open the home directory of the script, you should be able to find this file (nginx.conf):
enter image description here
I am trying to put the nginx.conf into my Vhost config
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
{{ssl_certificate_key}}
{{ssl_certificate}}
server_name ian.ph;
{{root}}
{{nginx_access_log}}
{{nginx_error_log}}
if ($bad_bot = 1) {
return 403;
}
if ($scheme != "https") {
rewrite ^ https://$host$uri permanent;
}
location ~ /.well-known {
auth_basic off;
allow all;
}
{{basic_auth}}
try_files $uri $uri/ /index.php?$args;
index index.php index.html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri =404;
fastcgi_read_timeout 3600;
fastcgi_send_timeout 3600;
fastcgi_param HTTPS $fastcgi_https;
{{php_fpm_listener}}
{{php_settings}}
}
location / {
if (!-e $request_filename){
rewrite ^/$ /index.php?link1=home;
}
if (!-e $request_filename){
rewrite ^/aj/([^/.]+)/?$ /ajax.php?type=$1&first=$2;
}
rewrite ^/#([^\/]+)(\/|)$ /index.php?link1=timeline&id=$1;
if (!-e $request_filename){
rewrite ^/([^\/]+)(\/|)$ /index.php?link1=$1;
}
}
location /reset {
rewrite ^/reset-password/([^\/]+)(\/|)$ /index.php?link1=reset-password&code=$1;
}
location /confirm {
rewrite ^/confirm/(.*)/(.*)$ /index.php?link1=confirm&code=$1&email=$2;
}
location = /two_factor_login {
rewrite ^(.*)$ /index.php?link1=two_factor_login;
}
location = /two_factor_submit {
rewrite ^(.*)$ /index.php?link1=two_factor_submit;
}
location /v {
rewrite ^/v/(.*)$ /index.php?v=$1;
}
location /api {
rewrite ^/api/v(([0-9])([.][0-9]+))(\/|)$ /api.php?v=$1;
}
location /admin {
rewrite ^/admin-cp$ /admincp.php;
rewrite ^/admin-cp/(.*)$ /admincp.php?page=$1;
}
location /admin-cdn/ {
alias /admin-panel/;
}
location /videos {
rewrite ^/videos/category/(.*)/rss(\/|)$ /index.php?link1=videos&page=category&id=$1&feed=rss;
rewrite ^/videos/category/(.*)/(.*)$ /index.php?link1=videos&page=category&id=$1&sub_id=$2;
rewrite ^/videos/category/(.*)$ /index.php?link1=videos&page=category&id=$1;
rewrite ^/videos/(.*)/rss(\/|)$ /index.php?link1=videos&page=$1&feed=rss;
rewrite ^/videos/(.*)$ /index.php?link1=videos&page=$1;
}
location /articles {
rewrite ^/articles(\/|)$ /index.php?link1=articles;
rewrite ^/articles/category/(.*)(\/|)$ /index.php?link1=articles&category_id=$1;
rewrite ^/articles/read/(.*)(\/|)$ /index.php?link1=read&id=$1;
}
location /aj {
rewrite ^/aj/([^/.]+)/([^/.]+)/?$ /ajax.php?type=$1&first=$2;
rewrite ^/aj/([^/.]+)/([^/.]+)/([^/.]+)/?$ /ajax.php?type=$1&first=$2&second=$3;
}
location /edit {
rewrite ^/edit-video/(.*)?$ /index.php?link1=edit-video&id=$1;
}
location /video_text {
rewrite ^/video_text/(.*)?$ /index.php?link1=video_text&id=$1;
}
location /watch {
rewrite ^/watch/([^\/]+)(\/|)?$ /index.php?link1=watch&id=$1;
rewrite ^/watch/([^\/]+)/list/([^\/]+)(\/|)?$ /index.php?link1=watch&id=$1&list=$2;
}
location /embed {
rewrite ^/embed/(.*)?$ /index.php?link1=embed&id=$1;
}
location /resend {
rewrite ^/resend/(.*)/(.*)?$ /index.php?link1=resend&id=$1&u_id=$2;
}
location /redirect {
rewrite ^/redirect/(.*)?$ /index.php?link1=redirect&id=$1;
}
location /settings {
rewrite ^/settings/(.*)/(.*)$ /index.php?link1=settings&page=$1&user=$2;
rewrite ^/settings/(.*)$ /index.php?link1=settings&page=$1;
}
location /terms {
rewrite ^/terms/([^\/]+)(\/|)$ /index.php?link1=terms&type=$1;
}
location /go_pro {
rewrite ^/go_pro(\/|)$ /index.php?link1=go_pro;
}
location /ads {
rewrite ^/ads(\/|)$ /index.php?link1=ads;
rewrite ^/ads/create(\/|)$ /index.php?link1=create_ads;
rewrite ^/ads/edit/(\d+)(\/|)$ /index.php?link1=edit_ads&id=$1;
rewrite ^/ads/analytics/(\d+)(\/|)$ /index.php?link1=ads_analytics&id=$1;
}
location /contact {
rewrite ^/contact-us(\/|)$ /index.php?link1=contact;
}
location /messages {
rewrite ^/messages/(.*)$ /index.php?link1=messages&id=$1;
}
location /view_analytics {
rewrite ^/view_analytics/(.*)$ /index.php?link1=view_analytics&id=$1;
}
location /video_studio {
rewrite ^/video_studio/(.*)$ /index.php?link1=video_studio;
}
location = /comments {
rewrite ^(.*)$ /index.php?link1=comments;
}
location = /dashboard {
rewrite ^(.*)$ /index.php?link1=dashboard;
}
location = /popular_channels {
rewrite ^(.*)$ /index.php?link1=popular_channels;
}
location = /create_article {
rewrite ^(.*)$ /index.php?link1=create_article;
}
location = /my_articles {
rewrite ^(.*)$ /index.php?link1=my_articles;
}
location /edit_articles {
rewrite ^/edit_articles/(.*)?$ /index.php?link1=edit_articles&id=$1;
}
location = /age_block {
rewrite ^(.*)$ /index.php?link1=age_block;
}
location /site {
rewrite ^/site-pages/(.*)$ /index.php?link1=site-pages&page_name=$1;
}
location /post {
rewrite ^/post/(.*)(\/|)$ /index.php?link1=post&id=$1;
}
location /edit_activity {
rewrite ^/edit_activity/(.*)(\/|)$ /index.php?link1=edit_activity&id=$1;
}
location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm|webp|zip|swf)$ {
add_header Access-Control-Allow-Origin "*";
expires max;
access_log off;
}
if (-f $request_filename) {
break;
}
}
But stil doesnt work
Can someone please help me how i can put my nginx.conf into server
Heres my nginx.conf https://pastebin.com/raw/cx2h7gRu
Thanks

Nginx location regex not working for sub pages

I have two react applications, one at '/home/user/www' and one at '/home/user/builds/checkout'. I want any url starting with '/checkout' eg. '/checkout/complete', '/checkout/error' to use that application. I have the below setup in my Nginx config file:
root /home/user/www;
index index.html index.htm;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
}
location ~ ^/checkout?(.*)$ {
allow all;
root /home/user/builds;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
try_files $uri $uri/index.html =404;
}
location ~ ^.+\..+$ {
try_files $uri =404;
}
going to the url '/checkout' is working correctly but any other url begining with '/checkout' like '/checkout/complete' and 'checkout/error' are just returning a 404 page.
Your configuration looks too complicated. Can you try this one?
root /home/user/www;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
location /checkout/ {
root /home/user/builds;
try_files $uri $uri/ /checkout/index.html;
}
If the /checkout URI won't redirect you to /checkout/, add this:
location = /checkout {
return 301 /checkout/;
}

How to fix ngnix.conf for redirect from .html and index.html

I want to do redirect from urls, real page exist here - mysite.com/folder/index.html :
mysite.com/folder/index.html to mysite.com/folder/
mysite.com/folder.html to mysite.com/folder/
mysite.com/index.html to mysite.com
This is part of my config
server_name k.my.net www.k.my.net;
index index.html;
root /var/www/demo/k.my.net/current/public;
rewrite ^(.*/)index\.html$ $1;
rewrite ^(/.+)\.html$ $1/;
location / {
try_files $uri $uri/ =404;
}
Also try do it with:
location / {
try_files $uri $uri/ #htmlext;
}
location ~ \.html$ {
try_files $uri =404;
}
location #htmlext {
rewrite ^(.*)$ $1.html permanent;
}
3-rd solution ERROR_LOOP
location ~* ^/([a-zA-Z1-9_-]*/)index\.html$ {
return 301 $1;
}
location ~* ^/([a-zA-Z1-9_-]*/?[1-9a-zA-Z_-]*)\.html$ {
return 301 /$1/;
}
location ~* ^/([a-zA-Z1-9_-]*/?[a-zA-Z1-9_-]*)/$ {
try_files /$1.html /$1/index.html =404;
}
You could use a regular expression location to extract the part of the URI before the trailing / and use try_files to test the two alternatives. See this document for details.
For example:
location ~ ^(.*)/$ {
try_files $1/index.html $1.html =404;
}
The location also matches / which will satisfy your third requirement.
Your rewrite statements should be safe, but if they cause redirection loops, you may need to replace them with an if block and test the original request in $request_uri. For example:
if ($request_uri ~ ^([^?]*?)(/index|)(\.html)(\?.*)?$) {
return 301 $1/$4;
}

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

Resources