Hace unas semanas hablábamos de cómo hacer una redirección transparente en Apache en función de una cookie. Uno de los ejemplos que puse es el acceso a un entorno de test de nuestra aplicación. Un entorno de test al que podamos acceder nosotros como desarrolladores o incluso se lo podamos enseñar a un cliente final sin afectar al funcionamiento normal de la web (que por otro lado está funcionando).
A veces, podemos crear una dirección paralela tipo test.miweb.com, pero otras veces no es posible. Y decirle a todo el mundo que añada una dirección IP a su archivo hosts, y que nos haga caso, es complicado. Pensemos en aplicaciones que tienen una fuerte dependencia del host al que están vinculada. Un ejemplo sencillo es WordPress, muchas veces nos encontramos con plugins o temas que, tanto en su configuración como en otras tablas almacenan el host y se complicado estar todo el rato haciendo cambios en la base de datos. Pero no solo WordPress, esta necesidad nos puede surgir con multitud de aplicaciones.
Tabla de contenidos
Un poco de historia (personal)
Todo empezó buscando una manera de cambiar el DocumentRoot de un VirtualHost en función de una cookie. Aunque la propiedad DocumentRoot no soporta condiciones ni se puede cambiar tan fácilmente. Lo que debemos hacer es una redirección con proxy en función de una condición. Esta redirección puede ser a la misma máquina, o a otra diferente, incluso máquinas virtuales o contenedores docker, siempre que tengamos acceso por red. Estas máquinas también pueden ser privadas. Por lo que nuestro servidor principal puede tener un segundo interfaz de red que esté conectado a otro servidor y hacer el proxy hacia ese segundo servidor.
Así que, si en este momento tenemos suficiente con la máquina donde tenemos el servidor web principal podemos utilizar otro puerto en el que también levantaremos el servidor web. A mí me gusta el 180, por poner un puerto. Ese puerto debemos mantenerlo privado, para que el acceso sea solo dentro del host local y no esté expuesto a Internet (cosa que no queremos). Podemos elegir, si la página de pruebas está en otro host, perfecto. Si queremos que dicha web de pruebas esté en el mismo host, seguid leyendo.
Escuchando por el puerto 180
Lo primero es que Apache escuche en el puerto 180, aunque podríamos utilizar otro servidor web como Nginx, lighthttpd, etc. Aunque si lo que queremos es utilizarlo como entorno de test, es conveniente que utilicemos siempre el mismo servidor web (por lo que pueda pasar).
Aunque nuestra web la sirvamos por HTTPs. La redirección no hace falta hacerla también por HTTPs. Ya que la conexión segura se seguirá haciendo entre Apache y el usuario final que pide la web, y luego Apache hará una conexión no segura con él mismo para acceder a la web de pruebas. Puede que, según nuestros contratos con el cliente, o según lo paranoicos que seamos con la seguridad, nos interese también cifrar esta comunicación interna, pero eso ya es cuestión de gustos.
En Apache, la configuración puertos suele estar en /etc/apache2/httpd.conf, /etc/apache2/apache2.conf, /etc/apache2/ports.conf o incluso los archivos pueden estar situados en /etc/httpd/ . En mi caso, en la instalación en Ubuntu, suele estar en /etc/apache2/ports.conf, y finalmente lo podemos dejar así:
1 2 3 4 5 6 7 8 9 | Listen 80 Listen 180 <IfModule ssl_module> Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule> |
O incluso podríamos cercar más el acceso así, haciendo que solo escuchemos en el host local:
1 | Listen 127.0.0.1:180 |
Ahora, nuestro Virtualhost de la web quedaría así (lo pongo completo, con SSL y todo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | <VirtualHost *:80> ServerName www.miweb.com ServerAlias miweb.com ServerAdmin administrador@miweb.com DocumentRoot /var/www/miweb.com/www RewriteEngine On RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} "!/.well-known/acme-challenge/" RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301] ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log ombined # Include conf-available/php7.0-fpm.conf </VirtualHost> <VirtualHost *:180> ServerName www.miweb.com ServerAlias miweb.com DocumentRoot /var/www/miweb.com_testing/www ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined Include conf-available/php7.0-fpm.conf </VirtualHost> <IfModule mod_ssl.c> <VirtualHost _default_:443> ServerName www.miweb.com ServerAlias miweb.com ServerAdmin administrador@miweb.com DocumentRoot /var/www/miweb.com/www ProxyPreserveHost On RewriteEngine On RewriteCond %{HTTP_COOKIE} cookie=([^;]+) RewriteCond %1 ^pruebas$ RewriteRule ^/(.*) http://127.0.0.1:180/$1 [P,L] ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined Include conf-available/php7.0-fpm.conf SSLEngine on SSLCertificateFile /etc/letsencrypt/live/miweb.com/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/miweb.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/miweb.com/fullchain.pem <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> </VirtualHost> </IfModule> |
Posibles cambios
Al estar utilizando otro VirtualHost, en este caso, el del puerto 180, podemos utilizar otra versión de PHP (En este caso, la web usa PHP), por supuesto otro DocumentRoot diferente, por lo que podríamos tener otra rama del repositorio de nuestra aplicación y la ésta podrá tener configuración de caché/logs/base de datos diferente a la aplicación principal.
Este sistema nos podría ayudar mucho a la hora de probar nuestra aplicación web en un entorno real o incluso con datos reales (si es posible), justo antes de desplegarla definitivamente.
Seguridad
Como dije antes, el puerto 180 no debe estar expuesto al exterior. No nos interesa que personas no autorizadas entren a nuestro sistema de pruebas. Podríamos tener contraseñas débiles, datos no reales o información de depuración activada que pueda dar pistas para atacar nuestra aplicación. Este puerto lo podemos bloquear desde nuestro proveedor de servicio, o desde la misma máquina, aplicando una restricción con UFW o desde iptables, así:
Ahora, debemos ser cuidadosos con las personas a las que les establecemos los valores de la cookie, porque podrán entrar a una zona privada de nuestro sistema. Sería conveniente no tener el acceso activado constantemente, sino activarlo solo cuando sea necesario, o poner también reglas por IP. Yo también, en los servicios donde utilizo esta técnica utilizo cookies como:
accesodepruebas=hn8JV9V1IfJylvQCmo+VUJb8ENgEIs/cbA8pWa7j1mE
La cadena aleatoria la podemos obtener así:
De manera que sea muy muy difícil para alguien hallar el valor adecuado de la cookie.
Foto principal: unsplash-logoPietro De Grandi
What a good and useful article. Can you let me share your post?
Its such as you learn my thoughts! You appear to understand so much about this, such as you wrote the guide in it or something. Newport roofing</a
Cookie testing is the type of software testing that checks the cookie created in the web browser. A cookie is a small piece of information that is used to track where the user navigated throughout the pages of the website.
I am confident you’ve got a great enthusiast following there. military pistol holster
I never knew about these things before, your article is quite informative, I should be lucky to have come across this, thanks for everything, if you have enough time, you can try playing basketball legends game, this game is really cool and fun.
Thank you for this article. Great work. It was very helpful.
One kind of software testing is called «cookie testing,» and it involves verifying the validity of a cookie stored in a web dordle browser. A «cookie» is a tiny file that stores information about a user’s actions on a website.
I appreciate the information and suggestions you have given me slope game. I will make an effort to understand it better for more.
Reading through a piece that provokes thought in its readers is one of my favorite things to do. In addition, many thanks for allowing me to share my thoughts here! five nights at freddy’s
The most popular pastime both in the US and throughout the world is playing slots at an onlinecasino https://ratemycasino.ca/reviews/caxino/. For slotplayers, the majority of US casinos provide bonuses and rewards. Most casino websites provide free spins and other welcome bonuses when you join up and make a deposit into your account.
I am a new user of this site so here i saw multiple articles and posts posted by this site,I curious more interest in some of them hope you will give more information on this topics in your next articles.
Really no matter if someone doesn’t be aware of after that its up to other users that they will help, so here it takes place 카지노사이트추천.
Thanks for sharing such great information with us. Your Post is beneficial and the information is reliable for new audience. Thanks again for sharing such a useful post. Siding Services
I really like your sharing, the information is really helpful to me. Hope you will have more interesting and interesting posts.
fireboy and watergirl
Whenever you need to uplift yourself, listen to your angels.
If I were you, I would incorporate more positive adjectives that begin with S into your vocabulary, as they help with lifting your spiriting or describing a person.
Thank you so much for sharing this great blog. Very inspiring and helpful too. Hope you continue to share more of your ideas. I will definitely love to read Sunderland roofers
Hello I am from impermeabilizar cubierta, I am glad to read the whole content of this blog and am very excited and happy to say that the webmaster has done a very good job here putting all the information content and information in one place, I will refer to this information with reference on my website
That’s so cool. Thanks for sharing! http://www.bestpainterboston.com
I’ve been looking for photos and articles on this topic over the past few days due to a school assignment, baccaratsite and I’m really happy to find a post with the material I was looking for! I bookmark and will come often! Thanks 😀
What a post I’ve been looking for! I’m very happy to finally read this post. 온라인카지노 Thank you very much. Can I refer to your post on my website? Your post touched me a lot and helped me a lot. If you have any questions, please visit my site and read what kind of posts I am posting. I am sure it will be interesting.
It is convenient that we always use the same web server in Baltimore.
That is definitely this sort of amazing inventiveness that you might be supplying so you offer the idea apart pertaining to free of charge. I want to share the landscaping design here. Let me know what you think.
Y decirle a todo el mundo que añada una dirección IP a su archivo hosts, y que nos haga caso, es complicado. | drywall repair fort worth tx
Este sería un sitio web muy interesante y me gusta cómo nos mostró incluso la codificación incluida en su sitio web. Para cualquier persona interesada, visite nuestro sitio web y obtenga más información sobre electrical contractors.
Nice! A cookie-based test environment is an effective way to ensure the functionality of a website.
There is an infinite number of ways in which you may personalize both your squad and your strategy, which will guarantee that the game will never get boring. color by number
I have been looking for articles on these topics for a long time. 바카라사이트 I don’t know how grateful you are for posting on this topic. Thank you for the numerous articles on this site, I will subscribe to those links in my bookmarks and visit them often. Have a nice day.
Thanks for posting Some Great ideas. Check this out http://www.tejadosansebastian.com