Jenkins links point to http instead of HTTPS resulting in login screen infinite loop - apache

For some reason Jenkins is redirecting the login screen back to the login screen when a successful login is made. See the attached packet trace. If I give the expected URL using https instead of HTTP all the pages load fine.
I have Jenkins configured behind a reverse proxy using apache. The proxy redirects traffic at /jenkins to :8080/jenkins. The base url is set to https://domain/jenkins and the jenkins --prefix parameter is set to /jenkins. I appreciate any help!

I found a work around. There was an error in my rewrite in the http virtualhost in apache. With the extra slash removed Jenkins no works but it is still sending http packets into the proxy server which then need to be rewritten to https instead of just posting all https links. Works but there is still and issue with Jenkins unfortunately.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}**/**$1 [R,L]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
The slash in the Rewrite rule was causing the URL to be rewritten slightly wrong. Most web application I've used previously can tolerate this error evidently as I have only encountered this problem 3 years after initially setting up the server.

Related

Had webserver "infinite redirect" errors, thought it was permissions or Apache, but it was a WordPress plugin setting

No matter what, I keep getting "ERR_TOO_MANY_REDIRECTS". Is there a "proper" way to redirect all http requests to https? (other than the five listed below...)
I have this:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
I also tried the top four answers in this article: http to https apache redirection. Nothing works, all infinite redirects.
Please try following:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
As others said, If you share you config file, it would be more easy to answer. Probably, some config cause a loop.
My problem was a WordPress plugin...
Solution: Set Paid Memberships Pro Stripe Gateway "force SSL" setting to "No"
Considerations that got me here:
1. EVERY suggestion for configs works, my config files work on nearly 100 sites I manage.
2. I have a near-identical WP site to this and it works.
3. I started re-activating plugins one by one.
The problem I found:
On first activation, Paid Memberships Pro detects http or https, then permanently disables "force SSL" setting on an https site. My working site had this happen, so no problem. My broken site had PMP installed on http, then I got the SSL cert later. SSL (Yes via JavaScript) created a non-breaking redirect loop. Turning off "force SSL" did the trick.
Moral of my story: When using WP, deactivate plugins before asking for help.

Apache Conditional Redirect Rule not forwarding header values

Goal:
To perform conditional redirect/forward from Site1 to Site2 based on specific header (HTTP_SM_USER) value of the request (in Site1) and ensure all the current custom header data is forwarded as part of the redirect/forward to Site2. The user can see the url change in the browser window after the redirect.
Flow:
User accesses Site 1 --> External Application Sets few custom headers in addition to default HTTP headers based on certain criteria --> Web-server looks for specific header value and if matches redirect all the headers to Site 2.
Apache Web server Config:
<VirtualHost *.443>
ServerName site1.com
ServerAdmin ashish#test.com
UseCanonicalName on
DocumentRoot /var/www/html
#
# SSL Config comes here
#
ErrorLog /path/to/error/log
TransferLog /path/to/access/log
LogLevel warn
RewriteEngine On
RewriteCond %{HTTP:SM_USER} ^USER1$ [NC]
RewriteRule .* https://site2.com/$1 [R=301, L]
<Location /page1>
.
.
</Location>
.
.
.
</VirtualHost>
Question: The Conditional Redirect happens but the headers are lost. What should I do to ensure the HTTP headers are forwarded as well ? I tried to see into apache mod_proxy but wasn't sure how in this scenario to use it. Experts please help. Any alternative suggestions are welcome too.
Solution:
I finally got it to work (with mod_proxy) after a lot of troubleshooting using the below flags and proxy rules. All Rewrite rules were removed.
ProxyRequests Off
ProxyPreserveHost On
SSLProxyEngine On
ProxyPass "/" "https://site2.com/" Keepalive=On
ProxyPassReverse "/" "https://site2.com/"
Note: User accesses Site 1 -> External app sets headers and apache immediately proxies to Site 2. Headers are now available on Site2.
You can't have the redirect "forward" headers, because it is up to the client what it sends to the next server after it has been issued a redirect. It may choose not to follow the redirect at all! Unlikely but just to make it clear what the situation is.
If you want the URL to change in the browser, then you need to do this on the application side. Have your application handle directing to the next server, and just issue a 200 response with an instruction for it to do so. Then it can send the custom headers as it did with the first request.
If that is not acceptable, then using mod_proxy could work as you said, but it would not change the URL in the browser, because the first server would be proxying the request to the second, so the browser would still see it as being the first.
In summary, you cannot do what you want to do, because HTTP does not work that way, so you are going to have to compromise in some way, either by not changing the URL in the browser, or by updating the application to take care of it.

Redirect Without changing URL Apache

I want to redirect one URL to another without changing the Browser URL
www.example.com/abc/(.*).xml should redirect to www.example.com/abc/xyz/index.htm?file=$1
But the Browser should display www.example.com/abc/(.*).xml
You can use a RewriteRule:
RewriteEngine On
RewriteRule /abc/(.*)\.xml$ /abc/xyz/index.htm?file=$1 [L]
Make sure you have mod_rewrite enabled and put this either in your VirtualHost config, or in a .htaccess file in your DocumentRoot
As Constantine posted on the accepted solution, the [P] flag is dangerous as it converts the server as a proxy.
See [this]: https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever?noredirect=1&lq=1
P = Proxy. Forces the rule to be handled by mod_proxy. Transparently provide content from other servers, because your web-server fetches it and re-serves it. This is a dangerous flag, as a poorly written one will turn your web-server into an open-proxy and That is Bad.

mod_rewrite Cannot redirect without rewriting url

I'm trying to redirect to another server application running on a different port of the same box with apache HTTPD as the frontend server running on port 80. I'm using the rewrite engine and the following syntax to redirect calls:
RewriteRule ^/?products/(.*)$ http://www.example.com:9000/$1 [L]
This works great, but the url changes to port 9000 in the browser. Whenever I change the [L] to a [P] for proxy I get a 404, not found error in the browser.
How do I fix this so that the url doesn't change in the browser, but that it still works?
You need to make sure you have mod_proxy loaded, otherwise the P flag won't work.
You can also use the ProxyPass directive as part of mod_proxy in your vhost config.

using proxy instead of redirection with htaccess rewriteRule

I'm developing a webapp and for the static files I'm simply using apache at localhost while the backend is on a couchdb instance running at localhost:5984.
The webapp interacts with files from the backend all the time. So what is happening when trying to test on apache all file requests to localhost:5984 are getting blocked due the cross-domain policy so the only way to get that working is starting the browser by setting flags to ignore that.
But again I get stuck when trying to test the app on mobile such ipad or iphone.
Currently I have this on my .htaccess file.
RewriteEngine on
# these are 302 http redirections instead of serving as a proxy
RewriteRule auth http://localhost:5984/auth [L]
RewriteRule db/([\s\S]+) http://localhost:5984/db/$1 [L]
RewriteRule send/([\s\S]+) http://localhost:5984/send/$1 [L]
# these are just redirections to static files and work great
RewriteRule ^([a-z/.]+) _attachments/$1 [L]
RewriteRule ^$ _attachments/ [L]
As you can see I have really no idea on how to deal with apache configuration unfortunately.
But what is happening right now is that for some of these rules apache is simply redirecting the page instead of provide it as a proxy server which causes the issue with cross-domain.
Also on the first auth rule I send POST and DELETE requests which as a redirection instead of proxy it won't pass the data being POSTed through.
So what I would like to achieve is to activate some kind of feature (if it exists) which will make apache simply render the page as it was on the localhost domain instead of redirect it. (I named this a a proxy, but perhaps that's not even the right term, sorry for any mistake committed with the nomenclatures).
Is is possible to achieve such action?
Thanks in advance
Have a look at these links / options:
[P] flag:
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_p
http://httpd.apache.org/docs/current/rewrite/proxy.html
mod_proxy (possibly -- but I think #1 should be enough if it's on the same server):
http://httpd.apache.org/docs/current/mod/mod_proxy.htm