meteor-up
meteor-up copied to clipboard
[Question] Redirect non-www to www
Is there a way to redirect non-www to www?
I've setup my DNS so both www and non-www are pointing to my app server, but I don't know if I can edit the Nginx config file to redirect non-www to www.
I can't use both as I am using Facebook login, which can only be set up for one domain.
what I did was keep my docker settings mostly as-is (defaults) and then add a new nginx config for a reverse proxy on the server that binds to 127.0.0.1:3000. In the reverse proxy I was able to easily configure nginx
I'm using "force-ssl" package
What is the canonical way to get this done? I'm curious about it and not very well versed with nginx.
Have you found a way yo solve this. Have same issue
Just to provide more context I tried this nginx.conf
:
server {
listen 80;
listen 443 ssl;
server_name awesome-vulcan.com;
return 301 https://www.awesome-vulcan.com$request_uri;
}
server {
listen 80;
listen 443 ssl;
server_name www.awesome-vulcan.com;
}
This fails because the SSL certificate is not associated with my NGINX server (or at least that's how I understand it). So the browser is not happy. If I remove the conf everything works fine, I am just unable to set up the redirection.
Edit: In case you need a fast solution, you can use this solution: https://blog.wax-o.com/2017/11/meteor-galaxy-redirect-non-www-to-www-with-ssl-and-https/ Here is the relevant code (thanks to the original author Fabien Huet):
WebApp.rawConnectHandlers.use(
(req, res, next) => {
/**
* Redirect non-www to www in production
*/
if (
process.env.NODE_ENV != 'development' &&
!req.headers.host.includes('www')
) {
res.writeHead(
301, {
'Location': 'https://www.mydomain.com' + req.originalUrl,
}
);
return res.end();
}
/**
* Keep going
* /!\ DO NOT DELETE /!\
*/
return next();
}
);
Theoritically it could be slower than NGINX based solution though, since you need to process every request to check the URL and it relies on JS.
Also wondering about this. I know this can be done at the DNS level but I'm curious to know how you'd do it with Nginx.
At the DNS level it is usually not a real redirection, only a CNAME/ALIAS. It may lead to issues with CORS because you are not really redirected. Some DNS providers seem to propose redirection but that's not DNS per se I think. So indeed the best solution is a server such as Nginx that will actually redirect you, I hope we'll manage to figure this out
Is there any solution to this? I am finding myself with the same problem.