Nginx rewrite arguments containing query strings with spaces

nginxregexrewrite

We have a SAS 9.4 VisualAnalyticsViewer page on a different host that I would like to present publicly over 443. The inside SAS server runs Apache Tomcat and listens on 8343 only. I would prefer not having to change every proxy_pass statement on the SAS box, or customizing its ports just to have a single page available over port 443 if possible. Presently our web servers have links using IFrames to the internal servers over 8343 which work but limits public user access because of remote firewall restrictions. I would like to place Nginx in front of the Tomcat server to have it listen on 443. My issue is with handling spaces in the rewrite statement. Can someone show me the proper syntax or regular expression on how to achieve this?

Please note; Bypassing Nginx and pasting the full Internal URL with spaces into a local browser does work.

Scenario for the application:

External Client Request:

https://apppub.domain.com/appVisualAnalyticsViewer Or,

https://apppub.domain.com/appVisualAnalyticsViewer/VisualAnalyticsViewer_guest.jsp?reportName=Report with Spaces&reportPath=/Some Data Path with Spaces/SubFolder/REPORT&appSwitcherDisabled=true

Nginx proxy checks for URI query string and then rewrites or appends it to an inside SAS server on port 8343.

Nginx Reverse Proxy:

https://appint.domain.com:8343/appVisualAnalyticsViewer/VisualAnalyticsViewer_guest.jsp?reportName=Report with Spaces&reportPath=/Some Data Path with Spaces/REPORT&appSwitcherDisabled=true

Client Response:

https://apppub.domain.com/appVisualAnalyticsViewer/ (Striping the query string from the remote client browser URL)

Below are the full configurations of apppub.conf(This web site only), response.conf, and proxy.conf which are all used by multiple sites on this same proxy server.


Nginx Config:

    upstream appint { 
    least_conn; 
    server appint.domain.com:8343; 
}

server {

      listen 80;
      server_name apppub.domain.com;

      access_log off;
      autoindex off;

      location / {
         # Rate limit first 6 requests and burst next 4. Reject everything else
         limit_req zone=one burst=10 delay=6;
         return 301 https://$server_name$request_uri;
      }

}

server {

    listen 443 ssl;

     server_name apppub.domain.com;

   # Site Specific Logging Metrics
     access_log /var/log/nginx/apppub.access.log main_ext;
     error_log /var/log/nginx/apppub.error.log warn;

    autoindex off;

   # Load SSL cert and session security
      include /etc/nginx/conf.add/domain-ssl.conf;

   # Add Security and Caching Headers to responses
     add_header X-Frame-Options SAMEORIGIN;
     add_header X-Content-Type-Options nosniff;
     add_header X-XSS-Protection "1; mode=block";
     add_header Cache-Control no-cache;
     add_header Vary Accept-Encoding;
     expires 5m;

   # Load client buffers and timeout response
      include /etc/nginx/conf.add/response.conf; (See Below)

   # Load Gzip mime type compression
      include /etc/nginx/conf.add/gzip.conf;


    # /appVisualAnalyticsViewer/

    location ~ ^/appVisualAnalyticsViewer {

       if ($args !~* "Report Name with Spaces"){
           set $report "VisualAnalyticsViewer_guest.jsp?reportName=Report with Spaces&reportPath=/Some Data Path with Spaces/REPORT&appSwitcherDisabled=true";
           rewrite ^(.*)$ $report break; 
           **(What's the correct syntax for a rewrite?)**
       }

       include /etc/nginx/conf.add/proxy.conf; (See Below)
       proxy_pass https://appint;

     }
Response Config:
Form POST submissions

client_body_buffer_size 1024K;
Form POST maxiumum size

client_max_body_size 16m;
Max client header size

client_header_buffer_size 2k;
Max time to receive client headers/body from buffer

client_body_timeout 5s; client_header_timeout 5s;
Max time to keep a connection open to clients

keepalive_timeout 5s;
Max time for the client accept/receive a response

send_timeout 5s;
Skip buffering for static files

sendfile on;
Optimize sendfile packets

tcp_nopush on; }
    Proxy Config:

    Proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header
    X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port;

    proxy_set_header Connection ""; # Enable keepalives proxy_set_header
    Accept-
    Encoding ""; # Optimize encoding proxy_set_header X-Forwarded-Host
   $host:$server_port; proxy_set_header X-Forwarded-Server $host;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_buffers 32 4m; proxy_busy_buffers_size 25m; proxy_buffer_size 512k;
   proxy_headers_hash_max_size 512; proxy_headers_hash_bucket_size 128;
   proxy_ignore_headers "Cache-Control" "Expires"; proxy_max_temp_file_size 0; proxy_next_upstream error timeout invalid_header http_500;
   proxy_connect_timeout 90; proxy_read_timeout 90; proxy_send_timeout 90; 
   proxy_intercept_errors off;

Best Answer

For those of you that may be interested. I figured out the syntax errors and ended up rectifying it using single quotes around the location.

'some string with spaces'