dikkini
dikkini

Reputation: 1182

NGINX rewrite rule changes URL

Nginx rewrite rule changes URL in browser. I saw this one question with the same problem, but it does not help me.

Nginx version:

# nginx -v
nginx version: nginx/1.10.1

Here is my configuration:

################################################
# PROXY CACHE OPTIONS ##########################
################################################
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=cache:30m max_size=1G inactive=60s;
proxy_temp_path /var/lib/nginx/proxy 1 2;
proxy_cache_key "$scheme$request_method$host$request_uri";
################################################

# Для заголовка WebSphere - WSIS
map $https $is_ssl {
  default false;
  on      true;
}

rewrite_log on;

large_client_header_buffers 4 64k;

#upstream websphere {
# # Health-monitored upstream groups must be stored in shared memory
# zone backend 64k;

# server s1erinyet:10039;
# server s2erinyet:10039;
#}


################################################
## SERVER SECTION START ########################
################################################
server {
  listen 80;

  server_name taliat.erp;

  ################################################
  ### SSL FOR ESIA ###############################
  ################################################
  listen 443 ssl;
  ssl_certificate /etc/nginx/ssl/nginx.crt;
  ssl_certificate_key /etc/nginx/ssl/nginx.key;
  ssl_session_cache    shared:SSL:10m;
  ssl_session_timeout  5m;

  ssl_ciphers  HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers  on;
  ################################################

  # Разрешаем только определенные методы
  if ($request_method !~ ^(GET|HEAD|POST)$ ) {
    return 444;
  }

  ################################################
  ## LOCATION / SECTION START ####################
  ################################################
  location / {

    # here is haproxy on 8888
    proxy_pass http://localhost:8888/;

    proxy_cache cache;
    proxy_ignore_headers Expires Cache-Control;
    proxy_cache_bypass $http_pragma;
    proxy_cache_revalidate on;
    proxy_cache_lock on;
    proxy_cache_min_uses 3;
    proxy_cache_valid 200 302 120m;
    proxy_cache_valid 404 1m;
    proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
    proxy_cache_methods GET HEAD;

    add_header X-Cache-Status $upstream_cache_status;

    set $no_cache "";
    # Any logged-in user's requests will not be cached and served directly by WebSphere. (Cookie based caching rule)
    #if ($http_cookie ~* "JSESSIONID*"){
      #set $no_cache 1;
    #}

    # These selected URI's below won't get cahced.
    if ($request_uri ~* "(/wps/contenthandler.*)") {
      set $no_cache 1;
    }

    proxy_no_cache $no_cache;
    proxy_cache_bypass $no_cache;

    # http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
    proxy_redirect off;

    # Отключили буферинг чтобы портал не нервничал
    proxy_buffering off;
    proxy_read_timeout 300;
    proxy_connect_timeout 600;
    proxy_buffer_size   128k;
    proxy_buffers   4 256k;
    proxy_busy_buffers_size   256k;

    # intercept errors on nginx side
    #proxy_intercept_errors on;

    ################################################
    ### REWRITE RULES START ########################
    ################################################

    set $original_uri $request_uri;

    rewrite "^(\/)$" /wps/portal/ break;
    rewrite "^/something\/*$" /wps/portal/Home/something/ break;
    ################################################
    ### REWRITE RULES END ##########################
    ################################################

    ################################################
    ### WEBSPHERE HEADERS START ####################
    ################################################

    proxy_set_header X-Rewrite-URL $original_uri;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_set_header "$WSSC" $scheme;
    proxy_set_header "$WSPR" $server_protocol;
    proxy_set_header "$WSRA" $remote_addr;
    proxy_set_header "$WSRH" $host;
    proxy_set_header "$WSRU" $remote_user;
    proxy_set_header "$WSSN" $server_name;
    proxy_set_header "$WSSP" $server_port;
    proxy_set_header "$WSIS" $is_ssl;

    # Note that these vars are only available if
    # NGINX was built with SSL
    proxy_set_header "$WSCC" $ssl_client_cert;
    proxy_set_header "$WSCS" $ssl_cipher;
    proxy_set_header "$WSSI" $ssl_session_id;

    # No equivalent NGINX variable for these headers.
    proxy_hide_header "$WSAT";
    proxy_hide_header "$WSPT";
    proxy_hide_header "$WSFO";

    proxy_pass_header  Set-Cookie;
    proxy_pass_header  Set-Cookie2;
    ################################################
    ### WEBSPHERE HEADERS END ######################
    ################################################
  }
  ################################################
  ## LOCATION / SECTION END ######################
  ################################################


  ################################################
  ## ERROR PAGES SECTION START ###################
  ################################################
  error_page   500 502 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
    internal;
  }

  # error 503 redirect to errror503.html
  error_page 503 @maintenance;
  location @maintenance {
    rewrite ^(.*)$ /503.html break;
  }

  # error 404 page
  error_page  404  /404.html;
  location /404.html {
    root /usr/share/nginx/html;
  }

  error_page 404 = @foobar;

  location @foobar {
    return 301 /;
  }
  ################################################
  ## ERROR PAGES SECTION END #####################
  ################################################
}
################################################
## SERVER SECTION END ##########################
################################################

And i expect in browser to see: http://servername/something but instead of this i see http://servername/wps/portal/Home/something/. What i do:

  1. Save original URI and place it in header

    set $original_uri $request_uri; proxy_set_header X-Rewrite-URL $original_uri;

  2. Set X-Rewrite-URL as $request_uri before and after rewrite

    proxy_set_header X-Rewrite-URL $request_uri;

Same configuration in Apache works perfectly:

RewriteEngine on
RewriteRule ^/something\/*$ /wps/portal/something [L,PT] 

UPDATE #1: I try to do it using location and proxy pass

location / {
    ....
    location /opendata {
        proxy_pass http://localhost:8888/wps/portal/Home/opendata/;
    }
    ....
}

It does not help me too

Upvotes: 0

Views: 1820

Answers (1)

Joshua DeWald
Joshua DeWald

Reputation: 3219

The proxy_pass directive can handle this case.Note that the comment from @richard-smith is also correct, which leads me to think that perhaps your instance is not fully reloading between configuration changes or there is something missing from your problem description.

Upvotes: 0

Related Questions