I have a java spring application deployed in a tomcat 8 environment in Amazon elastic beanstalk server. The application has HTTPS configured with proper certificate. When a socket connection connect the application I am getting below error in log
ERROR o.s.w.s.s.s.DefaultHandshakeHandler - Handshake failed due to invalid Upgrade header: null
I tried to figure out over internet, some post is saying it need to enable HTTPS. HTTPS is already enabled and normal webservice calls to the same server works.
Please let me know if anyone has any idea. Thank you !
The problem is the elastic beanstalk uses a proxy, so you can configure the proxy to support sockets.
The default proxy to Tomcat is Apache, I have changed it to nginx with the next file:
.ebextensions\nginx-proxy.config
option_settings:
aws:elasticbeanstalk:environment:proxy:
ProxyServer: nginx
then I added my nginx file:
.ebextensions\files.config
files:
"/etc/nginx/conf.d/01_websockets.conf" :
mode: "000644"
owner: root
group: root
content : |
worker_processes 1;
events {
worker_connections 2024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
# prevents 502 bad gateway error
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
# enables WS support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
Good luck!!!
Related
I have a java (spring) application running on Tomcat that needs to consume data from a Postgres DB that is proxied by a NGINX. Postgress doesn't use SSL and the connection from the java app to the NGINX should be over SSL.
Is there a way to do that ?
Go to this address : /etc/nginx/sites-available
create api.postgres.com file
server {
listen 80;
listen [::]:80;
server_name api.postgres.com;
location / {
proxy_pass http://127.0.0.1:5432;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
This should route the request towards running instance of postgres that will be running on port 5432.
Make sure your domain api.postgres.com must point to your server on which your database is installed.
I never did that, but I think this should work.
Thanks.
I am trying to proxy tomcat using nginx. I have this configuration below. Now the problem I am facing is if I access the url by IP(192.168.2.6) then it gets redirected to /auth_app is tomcat which is perfectly fine. The tomcat url redirect to a third party SAML2 provider and once it authenticated it redirected to http://localhost:8080/auth_app/ instead of http://192.168.2.6/auth_app/ and because of that my application doesn't open as tomcat is running on 192.168.2.6 and not localhost.
server {
listen 80 default_server;
server_name _;
error_log /var/log/nginx/abc_error.log;
access_log /var/log/nginx/abc_access.log;
rewrite ^/?$ /auth_app;
location /auth_app {
proxy_redirect off;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/auth_app/;
}
}
There should be a config setting in your tomcat app to set the redirect URL to http://192.168.2.6/auth_app/.
I need to mock certain http service running on different ports and IP address on linux system.
I have used embedded jetty server to mock http services locally on my system.
The problem statement I am having is:
http request from my system to specific Ip and port should be redirected to embedded jetty servers running on my local system
Example:
Request to http://10.10.10.10:8443 ----should be redirected to ---> http://localhost:8443
one way of achieving this by adding entry in /etc/hosts file
10.10.10.10 localhost
But I can't change my system's /etc/hosts file , is there any other way to achieve this programmatic.
Thanks in advance.
You put a nginx on the machine and listen on the 8443 or even port 80 and proxy your connection to the localhost.
server {
# listen on port 80
listen 80;
listen 443 ssl;
# for requests to these domains
server_name 10.10.10.10;
# keep logs in these files
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# SSL Certificate
ssl_certificate /etc/nginx/ssl/certs/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
# You need this to allow users to upload large files
# See http://wiki.nginx.org/HttpCoreModule#client_max_body_size
# I\'m not sure where it goes, so I put it in twice. It works.
client_max_body_size 0;
location / {
proxy_pass http://localhost:8443;
proxy_redirect off;
proxy_read_timeout 5m;
# make sure these HTTP headers are set properly
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I have a webapp deployed on a wildfly 10.1 application server.
This webapp is listened on: http://localhost:8080/app-profile-jsp/ (1)
I successful installed nginx. The server is successfull installed and I test to serve static web pages and images.
Now I want configure nginx to acccess the web app from
"http://www.frizio.local" (2) url.
I configure this address in /etc/hosts.
My configuration in nginx is the following:
server {
listen 80;
server_name frizio.local www.frizio.local;
access_log /var/log/nginx/static.access.log;
error_log /var/log/nginx/static.error.log;
root /srv/http/static;
location / {
proxy_pass http://localhost:8080/app-profile-jsp;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
location /pics {
autoindex on;
autoindex_exact_size off;
}
}
When I try to access to (2) the server respond "404 not found".
Thanks in advance
I think you missed the trailing / of the proxypass :
proxy_pass http://localhost:8080/app-profile-jsp/;
An other thing, I don't think you need the proxy_redirect off; here.
Hope it helped !
I have implemented BE application with WebSockets support using Spring Boot. I use SockJS in order to connect to my WebSocket endpoint and during the connection process I get a following error:
error during WebSocket handshake: Unexpected response code: 400
but then (as you can see at the image above) everything is working fine and websocket opened.
Right now I don't understand what can be a reason of this issue and how to fix it. Please help.
UPDATED
Thanks for the paweln1986 help I have fixed version issue with SockJS lib but the issue with Unexpected response code 400 still exists:
I also using nginx in front of Tomcat 8. This is my nginx config:
server {
listen 443 ssl;
server_name myserver.com;
ssl on;
location /api/ {
proxy_pass_header X-XSRF-TOKEN;
proxy_pass http://localhost:8081/api/;
proxy_set_header Origin "http://localhost:8081/api/";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}
location /dashboard/ {
proxy_pass http://localhost:8081/dashboard/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /manager {
proxy_pass http://localhost:8081/manager/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root /srv/www/htdocs/myserver/;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /srv/www/htdocs/;
}
}
You have wrong version of SockJS.
On the browser side, applications can use the sockjs-client (version 1.0.x) that emulates the W3C WebSocket API
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
You are missing two things under your /api/ section
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
https://www.nginx.com/blog/websocket-nginx/
This complication also occurs if you are using SSL with Amazon load balancers. The listener of the elb should have an entry of SSL(instead of HTTPS) with load balancer port as 443 and instance protocol as TCP with instance port as 80 (Don't forget to add certificate)
This will enable WebSocket traffic to pass through.
For detailed reference :
https://blog.jverkamp.com/2015/07/20/configuring-websockets-behind-an-aws-elb/