NanoHttpd / nanohttpd

Tiny, easily embeddable HTTP server in Java.
http://nanohttpd.org
BSD 3-Clause "New" or "Revised" License
6.92k stars 1.69k forks source link

Redirecting from http to https in nanohttpd #538

Open JiashuGuo opened 5 years ago

JiashuGuo commented 5 years ago

Hi there, I am trying to automatically correct/add https at the beginning of the URL the user typed in, for example: no matter user type http://XXXX or just XXXX I would like to change it to https://XXXX.

Does any one know if nanohttpd support redirecting from http request to https? I have viewed lots of answer online and most of them related to use ./htaccess in Apache server, I am afraid Nanohttpd does't support that method.

Thanks for any discussion!

LordFokas commented 5 years ago

You need to be running two servers for that, because HTTP and HTTPS use different ServerSocket implementations, so they need to be on different ports (80 & 443 are the defaults).

This being done, you can simply have the HTTP server redirect everyone (using HTTP 3XX with Location headers) to the same path in the HTTPS server and you're set.

JiashuGuo commented 5 years ago

You need to be running two servers for that, because HTTP and HTTPS use different ServerSocket implementations, so they need to be on different ports (80 & 443 are the defaults).

This being done, you can simply have the HTTP server redirect everyone (using HTTP 3XX with Location headers) to the same path in the HTTPS server and you're set.

Thank you so much for the idea, I am trying with this in the App.

JiashuGuo commented 5 years ago

You need to be running two servers for that, because HTTP and HTTPS use different ServerSocket implementations, so they need to be on different ports (80 & 443 are the defaults).

This being done, you can simply have the HTTP server redirect everyone (using HTTP 3XX with Location headers) to the same path in the HTTPS server and you're set.

I tried to run with two server, the redirecting works well, but can't on default port, I have add redirect in IP table still seems doesn't work. Do you have any idea about running server under port 1024 in Linux?

NoahAndrews commented 5 years ago

Are you running the application as root? By default, processes need root privileges to bind to ports 1-1024 on Linux.

JiashuGuo commented 5 years ago

No, we don’t run it on as root for security consider, so I am looking for other solution like redirect in ip table.

On Tue, Apr 9, 2019 at 2:33 PM Noah Andrews notifications@github.com wrote:

Are you running the application as root? By default, processes need root privileges to bind to ports 1-1024 on Linux.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/NanoHttpd/nanohttpd/issues/538#issuecomment-481448856, or mute the thread https://github.com/notifications/unsubscribe-auth/AX-gazslpYuuxHgclfBpoGX_0wZCAGZLks5vfQc5gaJpZM4cZXIR .

JiashuGuo commented 5 years ago

The iptable redirecting works after I reboot the device.

On Tue, Apr 9, 2019 at 2:35 PM Jiashu Guo jiashuguo@email.arizona.edu wrote:

No, we don’t run it on as root for security consider, so I am looking for other solution like redirect in ip table.

On Tue, Apr 9, 2019 at 2:33 PM Noah Andrews notifications@github.com wrote:

Are you running the application as root? By default, processes need root privileges to bind to ports 1-1024 on Linux.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/NanoHttpd/nanohttpd/issues/538#issuecomment-481448856, or mute the thread https://github.com/notifications/unsubscribe-auth/AX-gazslpYuuxHgclfBpoGX_0wZCAGZLks5vfQc5gaJpZM4cZXIR .

LordFokas commented 5 years ago

Either use ports above 1024 or sudo. You don't really have other choices.