Sunday, May 5, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 504  / 2 Years ago, sun, may 8, 2022, 10:18:13

I'm trying to create alias for my local website which is runned by vagrant(virtual box), hence I want redirect any requests from site.ru to 192.168.10.10/site.ru via /etc/hosts file. In the hosts file I have the following:



...
192.168.10.10/site.ru site.ru


However it doesn't work as I want. When I go to site.ru I'm appear in the 'real' site. I hope anybody understand me to answer for my question..



What did I do wrong?



UPDATE:
Solution: How to setup mass dynamic virtual hosts in nginx?


More From » command-line

 Answers
4

You can definitely direct a host name to an IP address on your LAN with Ubuntu's hosts file, but it does not support the particular syntax you're using. From man hosts:




This file is a simple text file that associates IP addresses with
hostnames, one line per IP address. For each host a single line should
be present with the following information:



IP_address canonical_hostname [aliases...]



The hosts file in Ubuntu does not direct hostnames with other hostnames, but only with IP addresses.



Therefore, instead of writing:



192.168.10.10/site.ru    site.ru


You should instead write:



192.168.10.10            site.ru


This will cause site.ru to be resolved to 192.168.10.10 instead of whatever it would otherwise be resolved to via DNS.



However, this may or may not effectively direct all traffic that is (conceptually) associated with the site.ru domain name. In particular, it will typically not be sufficient to cause a domain like www.site.ru to be mapped to 192.168.10.10 as well. To achieve that, add www.site.ru explicitly:



192.168.10.10            site.ru   www.site.ru


(You can alternatively make a separate line for it, if you prefer. This line must also start with an IP address.)



But hosts will not direct traffic to a specific folder on a website.



If by 192.168.10.10/site.ru you mean the site.ru directory on the web server at 192.168.10.10 (that is, if you are using / as a path separator), you cannot use the hosts file to do this. hosts files do not facilitate that behavior on any operating system; it is fundamentally unrelated to what hosts files do.



The reason this cannot work is that hosts files redirect host names to IP addresses. But 192.168.10.10/site.ru is not an IP address. It's an IP address followed by directory retrieval information sent to the IP address through an application-layer protocol. See this article on the meaning of different parts of a URL (and how only one part is resolved via IP).



The proper solution in this sort of situation is to configure the web server to serve different content depending on which host name is used to access the site. This can work because the HTTP protocol includes the ability of web browsers to tell the web server what host name it is using (and essentially all web browsers send this information whenever it's applicable).



If you have the ability to make changes to the web server and need help setting up "virtual hosts," and the web server is an Ubuntu system, please feel free to post a separate question about that here.



If you don't have the ability to make changes to the web server, you should talk to whoever does.



If even that is absolutely not an option, then you should edit your question (or post a new one, your choice) to clarify your situation in detail. A messier workaround that fits your needs might be possible. It would probably involve an HTTP proxy server running on each client machine and redirecting HTTP traffic by URL.


[#27378] Tuesday, May 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
umplegitimat

Total Points: 137
Total Questions: 126
Total Answers: 118

Location: Saint Pierre and Miquelon
Member since Sat, Aug 21, 2021
3 Years ago
umplegitimat questions
;