How to use Reverse Proxy and enable CORS with NGINX?

6,018 views
Skip to first unread message

michael.hi...@gmail.com

unread,
Apr 4, 2017, 10:28:59 AM4/4/17
to cornerstone platform
I'm programming in NODEJS with cornerstone sdk which is a DICOM viewer 

Here is the link 

Https://www.npmjs.com/package/cornerstone-wado-image-loader 

And I'm running my NODEJS application on port 3000 to get the dicom of DCM4CHEE WADO service as follows 

Http: // localhost: 3333 / wado? RequestType = WADO & studyUID = 1.3.6.1.4.1.25403.166563008443.5076.20120418075541.1 & seriesUID = 1.3.6.1.4.1.25403.166563008443.5076.20120418075541.2 & objectUID = 1.3.6.1.4.1.25403.166563008443.5076.20120418075557. 1 & contentType = application% 2Fdicom & transferSyntax = 1.2.840.10008.1.2.1 

But the browser shows me the following error 

XMLHttpRequest can not load http: // localhost: 8080 / wado? RequestType = WADO & studyUID = 1.2.840.113704.1.111.5 ... 26513.429 & contentType = application% 2Fdicom & transferSyntax = 1.2.840.10008.1.2. In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http: // localhost: 3000' is therefore not allowed access. 

Then in the documentation says that you have to use reverse proxy and enable CORS for that INSTAL NGINX 

And this is my NGINX configuration 


Server { 

        Listen 80; 
        Server_name localhost; 

        Location / { 
            Proxy_pass http: // localhost: 3000; 
            Proxy_set_header HOST $ host; 
            Proxy_set_header X-Real-IP $ remote_addr; 
            Rewrite /o(.*) $ 1 break; 
            Add_header 'Access-Control-Allow-Credentials' 'true'; 
            Access-Control-Allow-Headers '' DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-$ 
            Add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
            Add_header 'Access-Control-Allow-Origin' '*'; 

        } 
     } 

But the browser still displays the error 


XMLHttpRequest can not load http: // localhost: 8080 / wado? RequestType = WADO & studyUID = 1.2.840.113704.1.111.5 ... 26513.429 & contentType = application% 2Fdicom & transferSyntax = 1.2.840.10008.1.2. In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http: // localhost' is therefore not allowed access. 


HEADER RESPONSE 

Content-Type: application / dicom 
Date: Sat, 01 Apr 2017 01:15:38 GMT 
Expires: 0 
Server: Apache-Coyote / 1.1 
Transfer-Encoding: chunked 
X-Powered-By: Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag = JBoss_4_2_3_GA 
Date = 200807181439) /JBossWeb-2.0 
REQUEST HEADER 

Accept: * / * 
Accept-Encoding: gzip, deflate, sdch, br 
Accept-Language: en-US, en, q = 0.8, en-US; q = 0.6, en; q = 0.4 
Connection: keep-alive 
Host: localhost: 8080 
Origin: http: // localhost: 3000 
Referer: http: // localhost: 3000 / 
User-Agent: Mozilla / 5.0 (X11; Linux x86_64) AppleWebKit / 537.36 (KHTML, like 
Gecko) Chrome / 55.0.2883.87 Safari / 537.36 

As I do to enable the CORS in port 8080 where it is dcm4chee with the serialized WADO to get the DICOM? 

I'm sorry for my English

Leonardo Campos

unread,
Apr 4, 2017, 11:35:01 AM4/4/17
to michael.hi...@gmail.com, cornerstone platform
Sorry for the long delay!
I didn't reply to your message in the other thread.

If you have DCM4CHEE running on port 8080, you can set up nginx to accept requests on some other port (eg: 8081) and proxy the request to DCM4CHEE on port 8080.

Client (browser, XHR request) <--> nginx (8081, CORS) <--> DCM4CHEE (8080)

You should have something like this in your nginx.conf:

server {
    listen 8081;
    server_name localhost;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Origin,X-Auth-Token,Aut    horization,Accept,Client-Security-Token';
        add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, POST, PATCH, PUT, DELETE';
    }
}

(This is what I use to load studies from Orthanc. I just updated the port numbers)

The only thing you need to change in your node app is update the WADO urls in order to return the right ones to the client pointing to 8081 instead of 8080 like this:

http://localhost:8081/wado?RequestType=WADO&studyUID=1.2.840.11...

I hope this will help.

Leonardo da Silva Campos
Minas Gerais - Brazil

--
You received this message because you are subscribed to the Google Groups "cornerstone platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cornerstone-platform+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cornerstone-platform/c71b3354-dc8f-4436-97b6-3fa0ed38ecfb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

michael.hi...@gmail.com

unread,
Apr 4, 2017, 12:09:25 PM4/4/17
to cornerstone platform, michael.hi...@gmail.com
Thank you very much, it worked perfectly!
Reply all
Reply to author
Forward
0 new messages