NGINX Config Help (redirects)

Discussion in 'Nginx' started by eXthus., Oct 12, 2012.

  1. #1
    Hi there,

    I need someone who has experience with Nginx and can help with an nginx config file to get some redirections setup.

    Thanks,
     
    eXthus., Oct 12, 2012 IP
  2. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #2

    if you post your actual problem, what exactly you want to achieve and supply enough information about your environment, then I'm sure someone will help you with this or at least point you to the right direction.

    anyhow, what kind of redirection you want to setup?
     
    RoseHosting, Oct 12, 2012 IP
  3. eXthus.

    eXthus. Greenhorn

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    At the moment we have this:

    # nginx configuration


    location / {
    if ($http_host !~ "static([0-9]+)\."){
    rewrite ^(.*)$ htp://www.domain.com break;
    }
    if ($http_host ~ "static([0-9]+)\."){
    rewrite ^(.*)$ htp://www.domain.com$request_uri redirect;
    }
    }


    ...and we need this edited so that it will redirect all calls to static.domain.com to the main domain, EXCEPT for images.. in case of images it needs to check if the file exists on static.domain.com/imagepath and if it does it loads it from there, if it doesnt exist there it loads it from domain.com/imagepath
     
    Last edited: Oct 12, 2012
    eXthus., Oct 12, 2012 IP
  4. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #4

    I'm not sure if I understand you well, but anyhow you should do something like this:

    server {
      listen 80;
      server_name mydomain.com;
      ....
      ....
    
      location ~ /test {
        try_files http://www.google.com$uri $uri/ @img_pass;
      }
    
      location @img_pass {
        rewrite ^(.*)$ http://yahoo.com$uri last;
      }
    
    }
    Code (markup):
    in this example, this means if I go to mydomain.com/test/not-here.jpg, it will first test if the file requested is available at http://www.google.com/test/not-here.jpg then it will proceed checking it at http://www.google.com/test/not-here.jpg/ and if it is not present there it will do the '@img_pass' which means will look for the file at http://yahoo.com/test/not-here.jpg .
     
    RoseHosting, Oct 12, 2012 IP
  5. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #5
    RoseHosting, Oct 12, 2012 IP
  6. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #6
    Try adding the following lines to your 'static.domain.com' configuration:

    server {
    server_name static.domain.com
    listen 80;

    location /
    {

    if ($request_uri ~* ^/images)
    {
    break;
    }

    rewrite ^/(.*)$ http://domain.com/$1 permanent;
    }
     
    RoseHosting, Oct 13, 2012 IP
  7. eXthus.

    eXthus. Greenhorn

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    Thank you for the help.

    With the latest code, will that only use the static.domain.com address for image files, and then redirect all other requests to the main domain.com? Or is it set to only load content from static.domain.com/images?
     
    eXthus., Oct 13, 2012 IP
  8. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #8
    All requests except requests to your 'images' directory will be redirected to domain.com
     
    RoseHosting, Oct 15, 2012 IP
  9. eXthus.

    eXthus. Greenhorn

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    Ok thank you, I will get this tested now. Is it ok to change:

    rewrite ^/(.*)$ http://domain.com/$1 permanent;

    to

    rewrite ^/(.*)$ http://www.domain.com/$1 permanent;

    ?
     
    eXthus., Oct 15, 2012 IP
  10. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #10
    RoseHosting, Oct 15, 2012 IP