-
I have the following Nginx config which i want it to apply 2 rules.Rule 1
When the URL request contains the subdirectory /shop to be 301 redirected to the frontpage of the website. For example: https://mywebsite.com/shop to become https://mywebsite.comlocation /shop {
rewrite ^(.*)$ https://v2.no7.de redirect;
}
Rule2
When the URL request contains the subdirectory with an extra subdirectory or file then dont rewrite anything. For example: https://mywebsite.com/shop/...location / {
if ($request_uri ~ "^/shop/(.*)$"){
rewrite ^/(.*) https://v2.no7.de/%1 redirect;
}
}
But when im importing this configuration on the ISPConfig panel i get an error that this code is not right and no other explanation given.Could someone help me figure out this one? Thank you in advance
-
Faisal Memon Theodor • 3 months ago Hi Theodor,
Sorry for the late reply, I missed this one. I think the below will work for both:
location = /shop {
return 301 https://v2.no7.de;
}
location /shop {
# do something else
}
-
-
Martin Müller • 2 years ago • edited I have the following problem and find no solution - i want to cloak some affilate links complete automaticly.
I want to use the following variable rewrite rule:
from:
/pl/whatever/ASIN/
to:https://www.amazon.de/gp/pr...
So completely without map - completely variable.The link structure is the same for all articles - the URL always begins with:
Domain.com/pl/whatever/
Whatever is a string that changes with each article.ASIN is the ASIN from the product and must be the same as the asin from the outgoing link
whereby the ASIN should always be taken over from the outgoing URL.
Is this construction possible?
It would be nice to hear from you!
-
Amir Rawdat Mod Martin Müller • 2 years ago Hello Martin, the closest thing you can do is redirect the old domain to new domain returning HTTP 301 status code with the following directive
rewrite ^ $scheme://www.amazon.de$request_uri
Where $request_uri contains the ASIN number
In this case http://Domain.com/pi/whatev... would be permanently redirected to http://amazon.de/pi/whateve...
-
-
John Brisbane • 2 months ago • edited I would like to pass all url's ending in /documents/ to my s3 bucket. I tried implementing
location /documents/ {
proxy_pass https://cgla.s3.amazonaws.c...
}but it is still giving a 404 error after I added this rule and tried to use my domain, cg-la.com, in place of cgla.s3.amazonaws.com
Any help is appreciated.-
Alessandro Fael Garcia John Brisbane • 2 months ago • edited Could you perhaps try hitting your documents location, say using
curl -v <server_ip>/documents
and check what endpoint is being hit by NGINX? Or alternatively check your NGINX access/error logs at/var/log/nginx
? That should give you a better understanding into what's going wrong with your `proxy_pass` directive.
-
-
Martin Held • 2 months ago I want to redirect permantently already at SE listet urls in a proper way.
/hilfe/fragen/ links should be redirected to /fragen/ with 301 redirection.
how can I make sure that a 301 redirection will be done then?
-
Alessandro Fael Garcia Martin Held • 2 months ago • edited Hi Martin, you can use regex matching on your location block to capture the block after `/hilfe/fragen/*` and return a 301 with `/hilfe/*`
location ~ ^/hilfe/fragen/(.*)$ {
return 301 $scheme://$server_name/hilfe/$1;
}
-
-
Jonathan Richter • 2 months ago • edited Hi,
what I'm trying to achieve is this kind of configuration:
whatever.domain.com redirects to domain.com/whatever
This is my conf:
server_name ~(.*)\.domain\.com$;
set $someword $1 ;
return 301 https://domain.com/$someword;or
server_name ~(.*)\.domain\.com$;
set $someword $1 ;
return 301 https://domain.com/$someword$request_uri;Unfortunately, both cases lead to a "too many redirects" error.
Any hint would be appreciated
Thank you, Jonathan-
Antonio Prado Jonathan Richter • 2 months ago hello,
Faisal is right.
this would be the correct configuration for your case:
server {
server_name domain.com;
}
server {
server_name ~(.*)\.domain\.com$;
set $someword $1 ;
return 301 https://domain.com/$someword;
}--
antonio -
Faisal Memon Jonathan Richter • 2 months ago I believe what is happening is the the redirect is still being picked up by the same virtual server that did the redirect. You might need a new virtual server with a server_name domain.com;
-
-
Sudheer Kumar • 3 months ago If customers in my website browse sample.com/abc/xyz it should load my static files and keep the same site like sample.com/abc/xyz with the static content .
. What rule shall I use in nginx?-
Tony Mauro Mod Sudheer Kumar • 3 months ago Hi -- I'm aftraid I don't understand what you mean by "keep the same site like sample.com/abc/xyz with the static content". Can you please explain further?
-
Sudheer Kumar Tony Mauro • 3 months ago I have two web pages one is like(ex) sample.com/abc/xyz and another one is sample1.com/abc/xyz.Whenever customers browse sample.com/abc/xyz it should get the page/staticfiles from the sample.com/abc/xyz domain and keeps remain the same site:sample/abc/xyz.
Hope you are clear now-
Tony Mauro Mod Sudheer Kumar • 2 months ago • edited Sorry for the delayed reply, and that I still don't understand what you are saying. My interpretation is that you are seeing that after NGINX delivers the requested content from sample.com/abc/xyz, it is somehow switching the user over to a different site, or changing the sample.com/abc/xyz site itself in an unintended way. I don't see how either thing would happen.
That said, I suspect that to fully understand your issue requires diving deeper into the details of your configuration, which is beyond the scope of this comments section. NGINX offers support as a commercial service (https://www.nginx.com/servi.... To learn more, please contact our sales team at https://www.nginx.com/conta... .
To get help from fellow community members, send email to nginx@nginx.org or check out https://forum.nginx.org. You can subscribe to the NGINX email list at http://mailman.nginx.org/ma.... I suggest that when you ask a question there you include the entire section of configuration that is not working as expected.
-
-
-
-
Hugo Deveza • 3 months ago Where do I include that file???? Location?
-
Faisal Memon Hugo Deveza • 3 months ago What file are you referring to?
-
Hugo Deveza Faisal Memon • 3 months ago Thanks for the reply my friend!
I need to create this rewrite rules on my NGINX server:
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=$2;html=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;But I dont know where to put this!! File location path ?? File name??
-
Faisal Memon Hugo Deveza • 3 months ago Typically NGINX config is store in /etc/nginx/ directory. Are you using a hosted NGINX server?
-
Hugo Deveza Faisal Memon • 3 months ago I'm trying to add sitemap.xml on my website, but It keeps showing 404 at .../sitemap.xml URL
-
Hugo Deveza Faisal Memon • 3 months ago I've already tried to insert that cod at nginx.conf , but nothing happens :(
-
Hugo Deveza Faisal Memon • 3 months ago Its dedicated server from digital ocean, in /etc/nginx i have the folders:
> conf.d
> sites-available
> sites-enable
> snippets
> ssland some files... like
fastcgi.conf
nginx.conf-
Tony Mauro Mod Hugo Deveza • 3 months ago • edited Hi Hugo:
If your original question is about where in the NGINX configuration to put the
rewrite
directive, the documentation says it can go in aserver
,location
, orif
block.Beyond that we can't really diagnose your issue without diving deeper into the details of your configuration and infrastructure. NGINX offers support as a commercial service (https://www.nginx.com/servi.... To learn more, please contact our sales team at https://www.nginx.com/conta... .
To get help from fellow community members, send email to nginx@nginx.org or check out https://forum.nginx.org. You can subscribe to the NGINX email list at http://mailman.nginx.org/ma....
-
-
-
-
-
-
chandra • 7 months ago Hi
I've two domains on nginx server. How do I route from one domain to another.
Example:
1. Domain 1 is running on localhost:8080/test/#/home (I have this already running on my server).
2. Now How do I add a second domain's location path such that it navigates to the second domain and loads the application. (The url should look like localhost:8080/test/#/seconddomain.)I'm able to get till localhost:8080/test/seconddomain. But what I exactly what is /#/ in between test and seconddomain.
-
Faisal Memon chandra • 7 months ago The # is interpreted by the browser as an offset within a page. You have to use a different character.
-
chandra Faisal Memon • 7 months ago • edited My domain 1 by default use hash based routing? Any other way to overcome this? FYI my domain 1 cannot be changed. Can make any changes to make it work in domain 2.
-
Faisal Memon chandra • 7 months ago Unfortunately not that I know of. The # and everything after it is not sent to the server, it's handled client side, so NGINX never sees it.
-
chandra Faisal Memon • 7 months ago Or how can I write a path at location to say nginx to looking into anything after
Ex: location /home/(anything after home should look into this files){
try_files Xyz.html
}-
Faisal Memon chandra • 7 months ago location blocks work similar to routing tables, they'll follow the most specific match. So location /home {} will match /home/123, /home/xyz, etc. If in addition if you have location /home/123 {} then /home/123 will match there because its more specific.
-
-
-
-
-
-
Aftab Ali • 8 months ago Useful information.
-
Martin Lerch • 8 months ago see moreHello all. I had a bit of an issue with nginx and magento 2. I have an old web store that has wonky URLs with get parameters. I needed to set up a URL "translate" from old URLs to new magento URLs. Whenever I try to pass the URL with the get values (like ?name1=value1&name2=value2 etc) magento seems to ignore all get variables, or sees them as invalid get variables and therefore displays a magento 404 page. To work round this I figure I would do the following:
1. rewrite incoming urls from:
/old_directory/page.php?name1=value1&name2=value2
to
/old_directory/page2.php/name1=value1/name2=value2
basically replacing the ? and & with a /
2. Then in Magento 2 I put this as the old incoming URL:
/old_directory/page2.php/name1=value1/name2=value2
redirect permanent to
/new/magento2/url
301Note: Wish Magento would simply work with /old_directory/index.php?name1=value1&name2=value2. But it doesn't.....
Right now I have this in my nginx.conf file:
rewrite ^(/old_directory/index.php)(.*) /old_directory/index2.php/name1=$arg_value1? permanent;
rewrite ^(/old_directory/info.php)(.*) /old_directory/info2.php/name1=$arg_value1&name2=$arg_value2? permanent;It seems to work but it seems crude. Not very elegant.
When it comes to Nginx it will not work if I don't rename the script file from index.php to index2.php - don't know why. Same thing with info.php to info2.php.
Also it would be nice to make name1 and name2 dynamic, or even better, if there is a way to simply replace the ? and & with a / doing a nginx rewrite for any requested URL that starts with /old_directory/
Any suggestions are welcome! Cheers,
Martin
-
Faisal Memon Martin Lerch • 8 months ago Hi Martin, Couple suggestions for you:
- Look at using break instead of permanent. permanent causes a redirect so if the redirected URI also contains index.php that could get you stuck in a redirect loop. That may be what's happening. break won't issue a redirect.
- You can look into the NGINX JavaScript module which has access to all requests args and allows you to use JavaScript to modify data. http://nginx.org/en/docs/nj...
-
-
Udeme Samuel • a year ago https://v2-staging-app.wing...
it goes to
https://v2-staging-app.wing...
same, for example for
https://v2-staging-app.wing...
->
https://v2-staging-app.wing...
how can i forward to the / root folder using in ningx.
-
Faisal Memon Udeme Samuel • a year ago Try something like this:
location = index.php {
return 301 $scheme://$host/index.php/de$args
}
-
-
Felix • a year ago I need to convert the URL for an IPN listener - so from
Old URL domain /?s2member_paypal_notify=1;
New URL domain /?optimizemember_paypal_notify=1;however because of the ? in the domain (I guess this is an argument) I'm lost. Nothing I try to do is working. How can I rewrite or return it?
e.g.
rewrite ^/?s2member_paypal_notify§ /?optimizemember_paypal_notify permanent;
does nothingalso the following is not working:
location = / {
if ( $arg_s2member_paypal_notify ) {
rewrite ^/s2member_paypal_notify ^/optimizemember_paypal_notify redirect;
}
try_files $uri $uri/ /index.php?$args;
}Would be really nice to get some help here. I'm completely lost.
-
Faisal Memon Felix • a year ago I would recommend changing the variable in the client/server. If you can't do that, this might work though i haven't tested it:
if ($args ~ s2member_paypal_notify) {
return 301 $scheme://$host$uri?optimizemember_paypal_notify=1
}-
Felix Faisal Memon • a year ago thanks a lot - I had come up with something similar in the meantime:
if ( $args ~* "s2member_paypal_notify=1" ) {
set $args "optimizemember_paypal_notify=1";
rewrite ^.*$ https://www.velomap.org/ redirect;
}if ( $args ~* "s2member_paypal_notify" ) {
set $args "optimizemember_paypal_notify";
rewrite ^.*$ https://www.velomap.org/ redirect;
}It should always be =1, but as I'm not fully sure I also put the second rule to catch without 1. It will forward, but the redirect in browser seems to lose the =? so I use the first rule for the main as it will always work
I'm also gonna five your solution a try, but I think without set $args command it will not work. And I cannot change the variable on the client. Paypal madness.
-
-
-
Denis Panarin • a year ago Hello. How to make a redirect from /example to /example-123?
I trylocation /example {
return 301 /example-123;
}But it this case the system creates redirect loop /example -> /example-123 -> example-123 etc
Thanks.
-
Denis Panarin Denis Panarin • a year ago I managed.
location = /example {
return 301 /example-123;
}Maybe it will be usefull to someone
-
-
Oncu • a year ago How to conversion this rule to nginx:
RewriteEngine On
RewriteRule ^convert/(.*)$ view.php?v=$1 [L]
RewriteCond %{THE_REQUEST} \s/+watch\?v=([^\s&]+) [NC]
RewriteRule ^ convert/%1? [R=301,L]-
Faisal Memon Oncu • a year ago I can't read apache rewrite rules very well. what is the before and after you are trying to acheive?
-
Faisal Memon Oncu • a year ago Also please see: https://www.nginx.com/blog/...
-
-
Venkat Madala • a year ago How can I write rewrite rule for,
http://www.example.com/test... to http://www.example.com/anot...
-
Faisal Memon Venkat Madala • a year ago NGINX doesn't recieve the "#" anchor tags, I believe its client-side only so you can't do a redirect based on it.
-
-
sunil kumar • 2 years ago see moreHello This is my nginx file, I am doing a force redirect only on my domain. Does anyone know how does my each service uses the same force direct from http to https, bcoz I have to change my each bit of code from http to https..? Do I need to change anything on my NGINX conf file..?
user nginx;
worker_processes 1;error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;events {
worker_connections 1024;
}http {
include /etc/nginx/mime.types;
default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';# map for enabling web socket
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}access_log /var/log/nginx/access.log main;
client_max_body_size 25M;
sendfile on;
tcp_nopush on;
tcp_nodelay on;keepalive_timeout 65;
#gzip on;
#include /etc/nginx/conf.d/*.conf;
index index.html index.htm;
server {
listen 80 default_server;
server_name dev.seacvl.com;if ($http_x_forwarded_proto = 'http') {
return 301 https://dev.seacvl.com$request_uri;
}# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;proxy_pass http://haystack-ui;
}location /?token {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;proxy_pass http://haystack-ui;
}location /uiviews {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;proxy_pass http://haystack-ui;
}location /images {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;proxy_pass http://haystack-ui;
}location /auth {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;proxy_pass https://keycloak:8443;
}location /v1/catalog {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;proxy_pass http://consul:8500/ui;
}location /ui {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;proxy_pass http://consul:8500/ui;
}location /stream-service {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;proxy_pass http://stream-service:8089;
}location /device-service {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;proxy_pass http://api-gateway:8765;
}location /config-service {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;proxy_pass http://api-gateway:8765;
}location /upload-service {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;proxy_pass http://api-gateway:8765;
}location /reservation-service {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;proxy_pass http://api-gateway:8765;
}location /token-conversion-service {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;proxy_pass http://api-gateway:8765;
}location /ffmpeg-service {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;proxy_pass http://api-gateway:8765;
}location /admin-portal {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;proxy_pass http://admin-portal;
}}
}-
Faisal Memon sunil kumar • 2 years ago I don't fully understand the question. If you have multiple virtual servers, each one will need their own 301 redirect.
miknik Faisal Memon • 2 years ago If your aim is to redirect all traffic to SLL for all virtual hosts then dont specify a server for port 80. Set the following as your only listen directive on port 80:
server {
listen 80 default_server;
return 301 https://$host$request_uri;
}Then specify all your virtual servers to listen on port 443 only.
-
-
Andrew Traub • 2 years ago How can I create a rewrite rule that says something like, if the url contains /wp-content/ the file (everything after the /wp-content/) needs to be served from /mnt/extra/wp-content?
Faisal Memon Andrew Traub • 2 years ago Try:
location /wp-content {
alias /mnt/extra/wp-content/;
}
Veröffentlicht 17.01.2020