lpsandaruwan
lpsandaruwan

Reputation: 818

Nginx proxy pass 404 error - CORS

I have configured a proxy_pass for an JAVA web service to enable cross origin. I can send a post request using the web service URL http://10.1.200.156:8080/ClientService/passwordReset/, but http://10.1.200.156/ClientService/passwordReset/ gives a 404 error. And client application gives, Response for preflight is invalid (redirect). After searching the web I added proxy_set_header Host $http_host; to the Nginx configuration, but still no luck. Below, the Nginx configuration. Please help.

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
    listen 80;
    server_name euca-172-16-10-214.eucalyptus.internal;

    location /ClientService/passwordReset/ {
        proxy_pass http://10.1.200.156:8080/ClientService/passwordReset/;

            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                #add_header 'Content-Length' 0;
                return 204;
                }

            if ($request_method = 'POST') {
                add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                }

            if ($request_method = 'GET') {
                add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                }
        }
    }
}

Upvotes: 2

Views: 3141

Answers (1)

Keenan Lawrence
Keenan Lawrence

Reputation: 1464

You shouldn't include the URI when you're proxying. Try the following:

proxy_pass http://10.1.200.156:‌​8080;

Upvotes: 3

Related Questions