Can't bind virtual host to different port with httpd (CentOS) - apache

I have a VDS with CentOS and Apache. I'm using virtual-based hosts so I have domain 'my.domain' and few subdomains like 'sub1.my.domain', 'sub2.my.domain' etc. Each of them have it own config file in /etc/httpd/conf/vhosts/.
I need to redirect all connections from sub1.my.domain:8080 to another.web.site:8080. So I've added this lines in .htaccess file at sub1.my.domain's root directory:
RewriteEngine On
RewriteCond %{SERVER_PORT} 8080
RewriteCond %{REQUEST_URI} ^/
RewriteRule (.*) http://another.web.site:8080
But it wouldn't work because web-server listen only to port 80. So I did modified /etc/httpd/httpd.conf this way:
Listen 80
Listen 8080
And did modified sub1.my.domain's vhost file. Instead of...
<VirtualHost myserverip:80>
ServerName sub1.my.domain
AddDefaultCharset off
AssignUserID www-root www-root
DirectoryIndex index.html
DocumentRoot /var/www/www-root/data/www/sub1.my.domain
...
</VirtualHost>
...it now contents
<VirtualHost myserverip:8080>
ServerName sub1.my.domain
AddDefaultCharset off
AssignUserID www-root www-root
DirectoryIndex index.html
DocumentRoot /var/www/www-root/data/www/sub1.my.domain
...
</VirtualHost>
So as described in official documentation I've added new port to listen in httpd configuration and setup new port to resolve in domain's virtual host configuration. But when I'm trying to open sub1.my.domain:8080 I get an error - browser can't resolve that address. I've even forward 8080 port through iptables, reboot whole server but nothing helped.
What I did wrong?

Whole day spent to understand that fail2ban blocks all connections to 8080. I've added rule to fail2ban table through iptables and everything worked like a charm.

Related

Apache not loading document root correctly

Apache shows default page and doesn't load site configuration. OS: Debian 10.
Site is enabled but somehow it doesn't show files from public_html folder. Any help is appreciated.
<VirtualHost *:443>
SSLEngine On
ServerAdmin admin#abc.com
ServerName abc.com
ServerAlias *.abc.com
DocumentRoot /home/xx/public_html
SSLEngine on
SSLCertificateFile /home/xx/ssl.cert
SSLCertificateKeyFile /home/xx/ssl.key
<Directory /home/xx/public_html>
Require all granted
</Directory>
ErrorLog /home/xx/logs/error.log
CustomLog /home/xx/logs/access.log common
LogLevel debug
</VirtualHost>
No enough reputation to comment, so I’m trying with an answer and will clean it up if useful.
No mention of what you’re finding, if anything, in your logs. I assume you’re accessing using HTTPS to be sure your requests are going to port 443, but if per chance you were not I would try that first by specifying the protocol when entering the URL in your browser - otherwise you are probably making your request to the server on port 80 and not 443 where your VirtualHost is listening.
http://example.com ====> browser sends request to port 80, default port for http
https://example.com ====> browser sends request to port 443, default port for https
Is there also a VirtualHost entry for port 80 to redirect those requests to 443? If your browser is trying to load it as http using port 80 first then perhaps that’s why you’re seeing the Apache default page as I believe the server will be attempting to serve from /var/www/html/ for requests on port 80 unless you have already pointed these elsewhere with another VirtualHost, etc.
An example of what I mean that I have in use; either the ReWriteEngine or the Redirect permanent may be redundant, but I can confirm it functions fine for me as follows:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
Redirect permanent / https://www.example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
For my setup, I preferred that the www subdomain be default and set up DNS intentionally to treat it as such, so the bare domain is ServerAlias in my instance.

Virtual host works on phone, but it loads the wrong thing

I have MAMP running in my computer and the virtual host project.dev configured and working correctly (have been working on the project for years). Now I added project.dev to the hosts file on my Android phone, pointing to my local IP.
I access http://project.dev in mobile Chrome and it reaches my MAMP just fine, but it loads whatever is in /htdocs (basically as if I accessed "localhost" on my computer's browser), instead of loading the virtual host. This is how I've defined the virtual host in my httpd.conf file:
NameVirtualHost project.dev
<VirtualHost project.dev:80>
ServerName project.dev
ServerAlias *.project.dev
DocumentRoot "/project/public/root"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^(.+)\.(js|css|png|jpg|swf|ico|txt|html|mp3)$
RewriteRule ^/(.+) /index.php [QSA,L]
AddDefaultCharset utf-8
</VirtualHost>
Why isn't it executing that rule and loading the correct file and defaulting to MAMP's htdocs directory?
<VirtualHost :80> # Specify IP here, not hostname. You can leave it at :80 too (Listen on any)
ServerName project.dev # For HTTP Host header
ServerAlias *.project.dev # Same thing. I'm not sure if wildcards work.
DocumentRoot /project/public/root
yada yada yada
</VirtualHost>

How to Configure Apache 2.2.14 on Ubuntu 10.04 to use SSL only (Redirect loop)

Good morning,
I'm new to managing Apache servers. We have one internal server in particular that we would like to listen ONLY on port 443. There is only one 'site' or singular configuration on this server called 'interweb'. I attempted to make the following change in our sites-available folder as shown below, but it results in a redirect loop:
<VirtualHost *:80>
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/htdocs (...)
Any assistance you can offer would be greatly appreciated!
You need to get rid of your first virtual host because this looks like it is your httpd.conf file, not .htaccess, then change * to your server's IP address and finally put
Listen 443
at the top of the file.
Finally restart Apache in a terminal:
sudo /etc/init.d/apache2 restart

VirtualHost setup always doesn't work

I'm trying to set up a virtualHost for mampstack (NOT MAMP). This is what I've done so far:
In my httpd.conf file I've checked
Listen 8080
This is correct (I'm listening to the port 8080, NOT 80).
Then I've uncommented: Include conf/extra/httpd-vhosts.conf in my httpd.conf file
In my hosts file I have added the following: 127.0.0.1 mext-pst.local.
In httpd-vhosts.conf I've added:
NameVirtualHost *:8080
<VirtualHost *:8080>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs"
ServerName 127.0.0.1
ServerAlias localhost
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN localhost
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs/mext-pst-dashboard/web"
ServerName mext-pst.local
ServerAlias mext-pst.local
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN mext-pst.local
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [P]
</VirtualHost>
Now when I go to http://mext-pst.local/ I just get an error of my browser that he can't connect with the page ... .
When I go to http://mext-pst.local:8080/ I get the following error:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /index.php.
Reason: DNS lookup failure for: mext-pst.local:8080
When I go to http://mext-pst.local:8080/index.php it works ...
Change 8080 to 80 its the default. But if you want your site to run on 8080, then you have to use it. Another solution might be to rewrite the url, that is when your server gets the url, it rewrites it with port number (8080).
First of all change Listen 8080 to Listen 80, as you want your application to be accessible only with http.
In your http-vhost.conf file put following lines (of course after removing previous changes). In the following configuration yourDefaultHttpFolder means the default http folder. You might have changed it. So correct it depending on your system.
<VirtualHost *:80>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs/yourDefaultHttpFolder"
ServerName 127.0.0.1
ServerAlias localhost
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN localhost
<Directory /Applications/mampstack-5.4.20-0/apache2/htdocs/yourDefaultHttpFolder>
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [P]
</Directory>
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs/mext-pst-dashboard/web"
ServerName mext-pst.local
ServerAlias mext-pst.local
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN mext-pst.local
</VirtualHost>
This configuration is working on my server, when ever I try to access using 80 it rewrites the URL to my 8080 port and I see the content of that folder, not the the default index page.
You've got to change the port to *:80 and also if you're going to use a different name then the servername make sure to take up NameVirtualHost *:80 in your httpd.conf.
Since you are not listening on port 80, getting an error when you go to the URL without a port seems to be the correct result, right?
And when you go to port 8080 you are getting a Proxy Error. Are you sure there is no other software running on port 8080 or your browser doesn't have a proxy entered? Apache would not be giving a Proxy Error. I suspect this error is coming from somewhere else.
About your configuration, I am not exactly sure what it is that you are trying to achieve but, if you are trying to get some documents served when you go to localhost:8080 and another set of documents served when you go to mext-pst.local:8080 than you are almost there, NameVirtualHost *:8080 is correct and needs to be there, remove redirect lines as you don't need them (unless my assumption is wrong).

Redirecting HTTP to HTTPS with Apache

I have an issue using mod_rewrite to force redirection of HTTP requests to HTTPS using Apache 2.2.22 on Ubuntu Server 12.04.
My /etc/apache2/sites-available/default file is as follows:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>
The HTTPS host is defined in default-ssl in the same directory.
Visiting the server's local IP address, the redirect appears to work fine. However, accessing it via the FQDN, it doesn't. Using the FQDN, the site is available at port 5443, which is mapped in the firewall to 443 on the server, so perhaps that has something to do with the problem. I cannot just use port 443 directly, as it is in use on this IP address by another server.
To further clarify, the following are valid links:
https://website:5443
https://192.168.200.80:443
The redirect works here:
http://192.168.200.80
But the following gives a 400 Bad Request, and this is where the redirect is needed:
http://website:5443/
"Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please."
This is totally possible. The following redirects all http to the https url.
<VirtualHost *:80>
ServerName mydomainname.com
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
make sure you load the rewrite module mod_rewrite and enable it.
Your problem here is the initial HTTP request: This won't work as the server won't understand it receiving the request on port 443 (as the response code suggests).
If no port is given, the protocol http defaults to port 80, https to port 443.
This is also the reason why your local redirect works. I bet, if you access the page through http://website/ (with proper port forwarding of port 80), it will work as well. Also note that your VirtualHost is only defined for port 80 anyway, so it won't be valid for requests sent to website:5443 (or website:443).
In general, you'd need a server accepting both HTTP and HTTPS requests on a single port. Not sure any popular server actually supports something like that, because (I think) it essentially violates the specs.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
if u want to redirect your site from http:// anything.example.com to https: //anything.example.com ... Just create a dedicated hosting .conf file as /etc/httpd/conf.d/dedicated.conf and other conf file as virtual.conf ... entries for dedicated.conf are as follows....
this is dedicated server hosting conf file for redirecting it to https...
<virtualhost *:80>
servername host.example.com
documentroot /var/www/html
rewriteengine on
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
sslcertificatefile /etc/pki/tls/certs/name.crt
sslcertificatekeyfile /etc/pki/tls/private/name.key
</virtualhost>
<directory /var/www/html>
allowoverride all
require all granted
</directory>
Alternatively as mentioned in comment below, we can use redirect also:
<virtualhost *:80>
servername host.example.com
documentroot /var/www/html
RedirectMatch / https://host.example.com:ANY_PORT/ #if there is specific port
sslcertificatefile /etc/pki/tls/certs/name.crt
sslcertificatekeyfile /etc/pki/tls/private/name.key
</virtualhost>
<directory /var/www/html>
allowoverride all
require all granted
</directory>