My web application has a sub-directory, 192.168.1.8:8088/messages, that I want to expose to the outside world as messages.mysite.com. I've gotten half way there but I seem to be stuck. My requirements are as follows
- Redirect the site from HTTP to HTTPS.
- As I cannot edit the links the web application generates, I need to be able to accept requests from the client such as
messages.mysite.com/messages?id=23023. - Do not allow reverse proxy access to the root web application,
192.168.1.8:8088or to any sub-directory other than192.168.1.8:8088/messagesand its children.
My current NGINX configuration has some issues.
The HTTP to HTTPS redirection only works for the base path,
messages.mysite.com. If an HTTP request likehttp://messages.mysite.com/messages?id=23023comes in, my server redirects tohttp://messages.mysite.com/messages/messages?id=23023which is incorrect. This is because I have/messagesin thereturn 301setting, but without it I don't know how else to redirectmessages.mysite.comto192.168.1.8:8088/messages.The HTTPS site allows access to the full web application,
192.168.1.8:8088, but I only want it to access192.168.1.8:8088/messages.
My NGINX site configuration file is below. Any help or pointing in the right direction would be greatly appreciated.
server { listen 80; server_name messages.mysite.com; return 301 https://$server_name/messages$request_uri; } server { listen 443 ssl; server_name messages.mysite.com; set $upstream 192.168.1.8:8088; ssl_certificate /etc/letsencrypt/live/messages.mysite.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/messages.mysite.com/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot location / { proxy_pass http://$upstream; proxy_buffering off; proxy_set_header Host $http_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 $scheme; } } Update for clarification:
Examples
https://messages.mysite.com => 192.168.1.8:8088/messages https://messages.mysite.com/messages?id=23023 => 192.168.1.8:8088/messages?id=23023 https://messages.mysite.com/messages/send_message => 192.168.1.8:8088/messages/send_message https://messages.mysite.com/calendar => 404 https://messages.mysite.com/notmessages=> 404
/to go to/messages, but not/?id=23023to go to/messages?id=23023? This looks like unnecessary complex setup.messages.mysite.comand notmessages.mysite.com/messages. Sadly, not my choice. As the application server includes a/messagespath in the links it sends out to the clients, the clients send requests with/messagesincluded. I need to remove the/messagesthe clients send as I'm already sending incoming traffic to192.168.1.8:8088/messagesvia the NGINX setup.