NGinx comments


  • 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.com

    location /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

    • 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!

      • 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.

        • Reply
        • Share ›
          • 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.

            • Reply
            • Share ›
            • 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.

              example: https://vpn-anbieter-vergleich-test.de/hilfe/fragen/mein-vpn-service-ist-mit-unitymedia-ist-sehr-langsam-warum/

              how can I make sure that a 301 redirection will be done then?

              • Reply
              • Share ›
                • 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;
                  }

                  • Reply
                  • Share ›
                  • 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

                    • Reply
                    • Share ›
                    • 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?

                      • Reply
                      • Share ›
                      • Hugo Deveza • 3 months ago

                        Where do I include that file???? Location?

                        • Reply
                        • Share ›
                          • Faisal Memon  Hugo Deveza • 3 months ago

                            What file are you referring to?

                            • Reply
                            • Share ›
                              • 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??

                                • Reply
                                • Share ›
                            • 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.

                              • Reply
                              • Share ›
                                • 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.

                                  • Reply
                                  • Share ›
                                    • 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.

                                      • Reply
                                      • Share ›
                                        • 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.

                                          • Reply
                                          • Share ›
                                            • 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
                                              }

                                              • Reply
                                              • Share ›
                                                • 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.

                                                  • Reply
                                                  • Share ›
                                          • Aftab Ali • 8 months ago

                                            Useful information.

                                            • Reply
                                            • Share ›
                                              • Martin Lerch • 8 months ago

                                                Hello 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
                                                301

                                                Note: 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

                                                  see more
                                                • Reply
                                                • Share ›
                                                  • 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...

                                                    • Reply
                                                    • Share ›
                                                    • 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.

                                                      • Reply
                                                      • Share ›
                                                      • 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 nothing

                                                        also 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.

                                                        • Reply
                                                        • Share ›
                                                          • 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
                                                            }

                                                            • Reply
                                                            • Share ›
                                                              • 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.

                                                                • Reply
                                                                • Share ›
                                                              • Denis Panarin • a year ago

                                                                Hello. How to make a redirect from /example to /example-123?
                                                                I try

                                                                location /example {
                                                                return 301 /example-123;
                                                                }

                                                                But it this case the system creates redirect loop /example -> /example-123 -> example-123 etc

                                                                Thanks.

                                                                • Reply
                                                                • Share ›
                                                                • 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]

                                                                  • Reply
                                                                  • Share ›
                                                                  • Venkat Madala • a year ago

                                                                    How can I write rewrite rule for,

                                                                    http://www.example.com/test... to http://www.example.com/anot...

                                                                    • Reply
                                                                    • Share ›
                                                                    • sunil kumar • 2 years ago

                                                                      Hello 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;
                                                                      }

                                                                      }
                                                                      }

                                                                        see more
                                                                        • 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?

                                                                            • Veröffentlicht 17.01.2020  
                                                                            Tags