Nginx https redirects - stop further rules processing - https

I'm trying to configure http and https redirects from an old site to a new one.
According to the rewrite directive docs:
If the replacement string begins with http:// then the client will
be redirected, and any further rewrite directives are terminated.
And I'm trying to achieve the same with https to no avail.
This is my server config:
listen 80;
listen 443 ssl;
server_name mydomain.com
rewrite ^/path/resource(.*)$ $scheme://newdomain.com/newpath/resource$1 permanent;
...
return 301 http://newdomain.com/newpath/;
Using http I get what I'm looking for: if I access mydomain.com/path/resource I'm redirected to newdomain.com/newpath/resource.
However, the same with https redirects me to http://newdomain.com/newpath/.
I have rewrite_log on and in both cases the rewrite rule is matched but the https protocol does not stop further rules processing.
I have the feeling that either I'm missing something really obvious or I'm not approaching this problem properly. I wouldn't mind doing this in any different way at all if it works.
Have any of you out there any idea on how to achieve the http redirect with https too?

I usually like to use return instead of rewrite for redirects, try matching the path with a location block
location ~ /path/resource(.*) {
return 301 $scheme://newdomain.com/newpath/resource$1;
}
I think this way you know for sure there will be no further processing, because it's only 1 line, try it and tell me how it goes.
PS: This will maintain the $scheme of the request, requests to http:// will be redirected to a http:// and https:// will be redirected to https://

Related

Yii2 https URLs do not work

I try to run my local copy of my yii2 site with https.
I use this in config to force http url to https
'on beforeRequest' => function ($event) {
if(!Yii::$app->request->isSecureConnection){
$url = Yii::$app->request->getAbsoluteUrl();
$url = str_replace('http:', 'https:', $url);
Yii::$app->getResponse()->redirect($url);
Yii::$app->end();
}
},
The only url I can reach is the home page i.e. a bare url such as
example.ext
Other URLs give
Not Found The requested URL /site/index was not found on this server.
When removing the 'onbeforerequest' in the config, I can reach every http URL.
Question: why https URLs become unreachable?
Eventually I made out that there was no url rewrite for pretty url in the virtualhost litening to 443 port.
Adding the recommended rewrite rule in it solved the problem.
#stfsngue: Thank you for comment
Do you see any particular reason for preferring .htacces to 'onbeforeRequest' to force https?

Magento redirecting AWS Load Balancer

Good afternoon,
I have a Magento installation with Nginx running on an Auto Scaling Group on AWS with a standard 3 instances. To redirect traffic I use a load balancer with SSL causing my structure looks as follows:
User> Load Balancer (Port 443)> Instance (Port 80)
I changed into my database in the table mg_core_config_data the URLs to use https.
The problem I think is happening is the following:
Every time I access my URL, Load Balancer attempts to fetch the bodies content to send me the information I am requesting, in this way, as I am using port 80, when the Load Balancer reaches my instance and attempts to load the Magento, the base_url that is in the database redirects to https. With this redirection, the process is repeated again because I'm redirected back to https: // and try to get the instance information on port 80 again. I think that every problem is among this base_url and I can not return the magento information with port 80.
Follow my nginx configuration:
server {
listen 80 DEFAULT_SERVER;
server_name _;
root /home/ubuntu/www/mysite;
index index.php index.html index.htm;
location / {
try_files $ uri $ uri / /index.php$is_args$args;
}
location ~ \ .php $ {
try_files $ uri /index.php = 404;
fastcgi_pass unix: /run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
fastcgi_params include;
fastcgi_cache CACHE;
fastcgi_cache_methods GET HEAD;
fastcgi_cache_valid 200 1m;
fastcgi_cache_bypass $ no_cache;
fastcgi_no_cache $ no_cache;
}
...
}
The error I'm getting accessing my load balancer via https:
Anyone have any suggestions or have been through this?
Thank you.
Looking at your description, the problem is that your instances believe they're accessed over HTTP (not HTTPS) and are redirecting to HTTPS.
This is not an ELB problem, the ELB just forwards the HTTP requests. It's also not an EC2 problem since EC2 doesn't operate on HTTP level.
The redirects are either caused by
- your nginx server
- your magento installation
Typically, ssl redirect behaviour is configured on web server level.
There are no redirects present in the nginx config snippet you shared. You didn't share the entire config file, so look for redirects or 301's or 302's in your nginx config file. Also investige other nginx config files and .htaccess files in your magento installation folder.
I'm not familiar with Magento, but Magento could also cause the http(s) redirects.
Make sure you set the 'outside' url (the one that is mapped to the ELB) as your base url, as this will be the base url from the end users' point of view.
On https://www.siteground.com/tutorials/magento/magento_ssl.htm, you can find instructions for configuring Magento for SSL. It might be worth the try to set the base URL to http:// and activate the 'secure urls' options.

Passenger Standalone - Force Redirect to SSL

I've started to use Elastic Beanstalk with Ruby + Passenger Standalone which seems awesome, however I want to redirect all HTTP to HTTPS, I haven't managed to find any resource at all about this. I've been looking at customizing the nginx.conf.erb but can't really find out what to do.
Note that I already have SSL working, I just need to make all requests redirecting to SSL.
Thanks,
Johan
server {
listen 80;
server_name my.domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name my.domain.com;
[....]
}
It seems to late to answer this question. Just for those who need to tackle this in the future:
Try adding "port": 80 in your Passengerfile.json, and uncommenting "config.force_ssl = true" in your config/environments/production.rb.
The first change will direct your browser's request (without specifying port or protocol, i.e. something like www.example.com) to passenger. The second line tells passenger to redirect any http request to https.

Firefox "Unable to connect" without www?

I wasn't sure whether to put this in Serverfault or on Stackoverflow; it doesn't seem to be a server issue so I though here would be best.
I am currently working on a university website, and for some reason Firefox refuses to load the site unless you use www (ex www.university.edu). Every other browser accepts university.edu and simply redirects to www.university.edu as nginx is setup to do. My nginx config:
server {
listen 80;
server_name university.edu www;
rewritei ^http://www.university.edu$request_uri? permanent;
}
server {
listen 80;
server_name www.university.edu static.university.edu m.university.edu www.university.com;
.
.
.
}
So what should happen is when a request comes in and is www.university.edu, the second block catches it and everything runs normally, but if a request comes in and is university.edu the first block catches it and redirects it to the second block. But for some reason Firefox is not doing this.
Any idea's what could be causing this issue?
Update 1:
rewritei is not mispelled. The university's nginx was changed before it was compiled to enable regex case insensitivity, and was placed under the function "rewritei". Also after playing around with the site I found figured out that if you visit the site at www.university.edu first, then try university.edu it will load, but if you clear the cache and try to visit university.edu it will not load until you visit www.university.edu.
You have a typo; "rewrite" and try removing the www.
server {
listen 80;
server_name university.edu;
return 301 http://www.university.edu$request_uri;
}
Also take a look at the pitfalls on rewrite - http://wiki.nginx.org/Pitfalls#Taxing_Rewrites

Nginx will not stop rewriting

I am attempting to configure an owncloud server that rewrites all incoming requests and ships them back out at the exact same domain and request uri but change the scheme from http to https.
This is failed miserably. I tried:
redirect 301 https://$hostname/$request_uri
and
rewrite ^ https://$hostname/$request_uri
Anyway, after removing that just to make sure the basic nginx configuration would work it as it had prior to adding the ssl redirects/rewrites it will NOT stop changing the scheme to https.
Is there a cached list somewhere in nginx's configuration that keeps hold of redirect/rewrite protocols? I cleared my browser cache completely and it will not stop.
AH HA!
in config/config.php there was a line
'forcessl' => true,
Stupid line got switched on when it received a request at the 443 port.
Turned off and standard http owncloud works and neither apache/nginx are redirecting to ssl.
Phew.

Resources