Apache2 reverse proxy config not working correctly - apache2

I have 2 web servers on my home network. My main server is at 192.168.18.2 and has 4 virtual domains that have been working fine for years. My home network DNS server gives out 192.168.18.2 for queries of larkat.com, www.larkat.com and emailserver.larkat.com.
My email server, in addition to postfix and dovecot runs Roundcube on an internal web server. The mail server is at 192.168.18.12. No there is no SMTP or IMAP access to this server from or to the internet. Delivery of email in and out is handled through other secure means.
Here is my reverse proxy config on the main server:
<VirtualHost *:80>
ServerName emailserver.larkat.com
ProxyRequests off
ProxyPreserveHost On
ProxyPass "/" "http://192.168.18.12:80/"
ProxyPassReverse "/" "http://192.168.18.12:80/"
</VirtualHost>
instead of getting the emailserver server I am getting the web site at latkat.com.
All computers are Debian Linux.
What am I not doing correctly?
I have 2 web servers on my home network. My main server is at 192.168.18.2 and has 4 virtual domains that have been working fine for years. My home network DNS server gives out 192.168.18.2 for queries of larkat.com, www.larkat.com and emailserver.larkat.com.
My email server, in addition to postfix and dovecot runs Roundcube on an internal web server. The mail server is at 192.168.18.12. No there is no SMTP or IMAP access to this server from or to the internet. Delivery of email in and out is handled through other secure means.
Here is my reverse proxy config on the main server:
<VirtualHost *:80>
ServerName emailserver.larkat.com
ProxyRequests off
ProxyPreserveHost On
ProxyPass "/" "http://192.168.18.12:80/"
ProxyPassReverse "/" "http://192.168.18.12:80/"
instead of getting the emailserver server I am getting the web site at latkat.com.
All computers are Debian Linux.
What am I not doing correctly?

Related

how to deploy Next.js website on apache webserver [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 months ago.
Improve this question
I made React.js project , but it needs server side rendering, So now I have to migrate from CRA to next.js . the problem: I have to upload my website on apache web server, from what I gathered from google, I need installed node.js and pm2 (latest versions), also configured apache for reverse proxy. I also created ecoystem for pm2(I guess it was necessary). it looks like this at the moment:
module.exports = {
apps : [{
name: "nextjs-app",
script: "npm",
args: "run build",
env: {
NODE_ENV: "production"
}
}]
};
but when I ran pm2 start npm -- start , terminal is giving me respone like this:
pm2 start npm -- start
[PM2] Spawning PM2 daemon with pm2_home=/home/georgianar/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /usr/local/bin/npm in fork_mode (1 instance)
[PM2] Done.
but when I try to see list of process, there is none, and when user tries to enter the website, site log shows that there is no service on port 3000
AH01114: HTTP: failed to make connection to backend: localhost
and
(111)Connection refused: AH00957: http: attempt to connect to 127.0.0.1:3000 (localhost:3000) failed
any idea why?
Well if you want to run both on the same server you can do it in many ways
To run both Node.js and Apache on the same server, follow these steps: https://nodejs.org/en/download/package-manager/
To run your Node.js application as a service, you can use multiple methods such as creating a service, using a process manager (PM2 is common), or running a script on server startup with a cron job. For more information, see this link: How do I run a node.js app as a background service?. You'll need a server start script, typically named server.js, to do this. An example can be found at Next.js: https://nextjs.org/docs/advanced-features/custom-server. To run the application manually in the background, navigate to the app directory and run node ./server.js &
Set up a tunnel using proxypass on Apache. This is commonly used to run the Node.js application on a specific URL. You'll need to install the Apache module mod_proxy and edit the configuration for your Apache server. An example configuration could look like this:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster#example.com
ProxyPreserveHost On
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
You can also wrap the proxypass to run under a specific path using the location tag, like this:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster#example.com
ProxyPreserveHost On
<Location "/mynodeapplication/">
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</Location>
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
This will run your application at https://yoururl/mynodeapplication. For more information, see this link: https://httpd.apache.org/docs/trunk/mod/mod_proxy.html.
I hope this covers most of it.
To run Next on Apache, you'll need to set up the Apache settings so that when the domain or subdomain you want Next to run on is accessed, Apache points the request to the local port on the machine that's running Next. Another issue is that Next's built-in server doesn't support SSL (which I'm assuming you want), and using a custom server instead (like Express) loses you many features and optimizations that would otherwise just work with the Next server. So you'll not only need to have Apache redirect to Next, but also have Apache handle the SSL certificate.
To do this, specify a VirtualHost for the server on port 80 to redirect to HTTPS. For example, at least in Ubuntu, in etc/apache2/sites-available/000-default.conf, you can add:
<VirtualHost *:80>
ServerName subdomain.example.com
Redirect / https://subdomain.example.com/
</VirtualHost>
And then set up the SSL certificate for SSL requests (on port 443), and tell Apache to route client requests to the local machine port, and to route the local machine port's responses back to the client. If you're using LetsEncrypt, you can add this into 000-default-le-ssl.conf:
<VirtualHost *:443>
ServerName subdomain.example.com
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://0.0.0.0:16534/
ProxyPassReverse / http://0.0.0.0:16534/
SSLEngine On
SSLProxyEngine On
SSLCertificateFile <insert path to fullchain.pem>
SSLCertificateKeyFile <insert path to privkey.pem>
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
Above, I'm running Next on port 16534 - this is the port you'd see Next logging when it starts:
ready - started server on 0.0.0.0:49447, url: http://localhost:16534
so substitute it with whichever port you're using.
You'll also need to make sure the DNS server for your website points users to your webserver's external IP address by adding an A record, if you don't have one already. If the Next app is to run on a subdomain, you'll need a separate A record for the subdomain.

Apache2: how to avoid that one website reply as default?

I have a server apache2 with a few of websites.
One of these is served both on http and https
All of others are server only on http
I inherited this bad situation from an ex worker of company whre I am imployed, do not blame me
If I try to access via https one of other website, apach2 is serving, i think, the first (and only) server accessible via https, even if it has a different domain.
If I access https://blablbalca.com, apache2 knows that blablbalca.com is not served on https, but It doesn't give me a 404, it serves https://anotherwebsite.com
What directive tells apache to automatically serves 'the first one' in case of missed definition?
How to fix?
EDIT: I found a more elegant and generalized way to express my question:
How can I avoid that apache2 serves a random virtual host if a domain, pointing to the same ip, is not really served from my server?
You can do a name best virtual host and set the ServerName and ServerAlias to match each website. Furthermore, if you want 'blablbalca.com' to return an error when accessed over HTTPs, you'll have to setup a virtual host in the your_site.conf to handle traffics coming from the https port. Like doing a redirection or showing an error page.
The default configuration file that deals with the incoming connection from a domain name that is not on the server or without domain name at all (direct IP connection):
For port 80:
<VirtualHost *:80>
ProxyPass "/" "http://your/path/here"
ProxyPassReverse "/" "http://your/path/here"
</VirtualHost>
For port 443 (SSL):
<VirtualHost *:443>
ProxyPass "/" "http://your/path/here"
ProxyPassReverse "/" "http://your/path/here"
</VirtualHost>
The website with a domain:
<VirtualHost *:80>
ServerName yoursite.com
ServerAlias www.yoursite.com
ServerAdmin none#webhost.com
DocumentRoot /var/www/yourWebsite
</VirtualHost>
I am removing a lot of directives and only keep the ones that we are focusing on in this case.

how to deploying nextjs app integrated with react and redux on cpanel

In react you can transfer static files in the build folder to cpanel, direct admin or other shared server.
What is the solution for nextjs?
without using the nodejs and install it on the server
If your application does not generate dynamic pages at runtime you can use nextjs export static which will generate static HTML for you and then you could deploy on cpanel
I found the way myself.
On a dedicated server, as long as you are connected to the server using Putty, you can load the site by creating a virtual host. by example
<VirtualHost *:80>
ServerName digiattar.com
ServerAlias www.digiattar.com
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
If you enter the domain address in the browser, the server points to the localhost address and port 8080, on which Nextjs is running.
After completing the steps, you can go to the root of your project and run Nextjs as like it is on your computer using the command npm run dev or next start or etc
but after close putty everything expire, using pm2 for this problem

Multiple Domains on a VPS with Apache

I am planning on getting a VPS soon and have two sites I want to host. I have my local vhosts setup for my development environment using host file to port it correctly.
My question is hopefully simple: When setting up with two separate domains that point to the one VPS server is Apache smart enough, like on the local environment, to automatically filter any requests for domain.com to the correct VHOST like it does locally? I would just like to be sure :)
Thanks!
Example of what I am asking:
Say Domain1.com and Domain2.com are both on my VPS.
When someone requests www.Domain1.com apache sees this and passes it through to the domain1.com vhost file.
When someone requests www.Domain2.com apache sees this and passes it through to the domain2.com vhost file.
Repasting my answer from Hosting two domains using only one VPS? as here it is even more relevant.
As complete beginner, I have been trying to host multiple domains on one Apache VPS. Tutorials had too much information that lead me to confusion.
Below I describe, for complete beginners, how to host multiple domains on one VPS server with Ubuntu and Apache.
IMPORTANT! You need to use root account to execute most operations.
IMPORTANT! If you have been trying to make some changes to apache configuration before, undo them.
Creating VirtualHosts
Create folders for your domains on server.
For example:
/home/apache/domain1
/home/apache/domain2
Put index.html file in each folder with any text.
This is domain1
This is domain2
Go to /etc/apache2/sites-available folder.
Create file domain1.conf
sudo nano domain1.conf
<VirtualHost *:80>
DocumentRoot /home/apache/domain1
ServerName domain1.com
ServerAlias www.domain1.com
</VirtualHost>
Create file domain2.conf
sudo nano domain2.conf
<VirtualHost *:80>
DocumentRoot /home/apache/domain2
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>
You can create subdomains same way.
sudo nano blog.conf
<VirtualHost *:80>
DocumentRoot /home/apache/blog
ServerName blog.domain.com
ServerAlias www.blog.domain.com
</VirtualHost>
Enable created sites
sudo a2ensite domain1.conf
sudo a2ensite domain2.conf
Restart apache
sudo service apache2 reload
Redirecting domain to server
Created VirtualHosts will work only if you redirect your domain name to server IP. Domains are just names that can be translated to IP numbers.
Local computer
To test your configuration on local machine, you need to edit hosts file.
sudo nano /etc/hosts
It should look like this.
127.0.0.1 localhost domain1.com domain2.com
Hosts file tells your computer that domain needs to be redirected to local machine.
IMPORTANT! If you create entry in hosts file for existing domain, for example
127.0.0.1 stackoverflow.com
you will loose access to this website.
Server
In order to redirect domain to you web server, you need to create or modify "A"-type DNS record for given domain to IP address of your server. You can do it by panel control provided by your domain registrar.
If you do not know IP address of your server, log in to that server and type in command line:
ifconfig
The simple answer is 'yes', Apache is that clever. If you are used to using a local vhost file in conjunction with your hosts file to simulate local domains, the exact same technique can be applied on a VPS. The part you are doing with the hosts file is essentially creating a local name server, other than that Apache doesn't know the difference. Simply set the ServerName directive for each named vhost and you should find it working the same as it does locally.

Redirecting https requests to two different weblogic servers using the Weblogic proxy and Apache2

I have a server serverA running a weblogic application App1, with base url /app1/ on port 7001, and another server serverB, running a weblogic application App2, with base url /app2/ on port 8001. Both servers run Solaris, Apache2 and Weblogic 9.2
(details changed, but these are representantive)
In other words, app1 could be accessed on
http://serverA:7001/app1/
and app2 on
http://serverB:8001/app2/
However, the customer requires that all access to the applications use https on port 443 to server1.
If there was only one application, I could use a virtual host and set the handler for URLS beginning with /app1/ to the Weblogic proxy, which would forward them on to server 1 on port 7001.
With two apps, I would need another virtual host with another Location statement matching /app2/ and forwarding to the current server on port 8001 ... but I don't see how this could work as the first virtual host will have done the SSL negotiation to determine the URL, and Apache presumably can't do that over when things fall through to the second virtual server.
So how do I handle this?
My current idea is to proxy all SSL requests arrving at server1 to the same server server1, on port 80 (so essentially just doing SSL termination), then adding two virtual hosts for the /app1/ and /app2/ URLs in the way described above.
Is this going to work? Have I missed something obvious about other ways of doing this?
EDIT: I think I may have missed that the Weblogic plugin can have several blocks each directing the page to different places. In which case this becomes easy.
I will test tomorrow when back at work and update the question
First, the must read resource for this is of course the official documentation : Installing and Configuring the Apache HTTP Server Plug-In (see also this previous answer for more links about the WLS 9 plugin).
As detailed in the section Configuring the Apache HTTP Server Plug-In, I'd define several IfModule, one for each application (clustered or not), and, indeed, several VirtualHost (which can include IfModule). There is an example in the documentation:
# VirtualHost1 = localhost:80
<VirtualHost 127.0.0.1:80>
DocumentRoot "C:/test/VirtualHost1"
ServerName localhost:80 <IfModule mod_weblogic.c>
#... WLS parameter ...
WebLogicCluster localhost:7101,localhost:7201
# Example: MatchExpression *.jsp <some additional parameter>
MatchExpression *.jsp PathPrepend=/test2
</IfModule>
</VirtualHost>
# VirtualHost2 = 127.0.0.2:80
<VirtualHost 127.0.0.2:80>
DocumentRoot "C:/test/VirtualHost1"
ServerName 127.0.0.2:80
<IfModule mod_weblogic.c>
#... WLS parameter ...
WebLogicCluster localhost:7101,localhost:7201
# Example: MatchExpression *.jsp <some additional parameter>
MatchExpression *.jsp PathPrepend=/test2
#... WLS parameter ...
</IfModule>
</VirtualHost> <IfModule mod_weblogic.c>
Note that this is a Multiple IP-Based Virtual Hosts configuration (and not Name-Based as stated in the documentation). But this is actually good because this is exactly what you need when using SSL as you can't use name-based virtual hosts. Quoting Why can't I use SSL with name-based/non-IP-based virtual hosts? from Apache's SSL/TLS Strong Encryption: FAQ
The reason is very technical, and a somewhat "chicken and egg" problem. The SSL protocol layer stays below the HTTP protocol layer and encapsulates HTTP. When an SSL connection (HTTPS) is established Apache/mod_ssl has to negotiate the SSL protocol parameters with the client. For this, mod_ssl has to consult the configuration of the virtual server (for instance it has to look for the cipher suite, the server certificate, etc.). But in order to go to the correct virtual server Apache has to know the Host HTTP header field. To do this, the HTTP request header has to be read. This cannot be done before the SSL handshake is finished, but the information is needed in order to complete the SSL handshake phase. Bingo!
So, in the sampel above, modify the virtual hosts IP addresses and ports, the ServerName, adapt the IfModule to suit your needs (and set up DNS entries to point on the IPs) and there you go.
I don't have any experience with weblogic, so maybe I'm missing something important. But this sounds like a straightforward application for apache's reverse proxy capability. Set up an apache instance serving https, and configure two locations as follows:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
<Location /app1>
ProxyPass http://serverA:7001/app1
ProxyPassReverse http://serverA:7001/app1
</Location>
<Location /app2>
ProxyPass http://serverB:8001/app2
ProxyPassReverse http://serverB:8001/app2
</Location>
The example config from the WebLogic 10.3.x documentation is a perfect fit for your question. Here it is with some other details added:
<VirtualHost _default_:443>
SSLEngine on
# other SSL options here...
LoadModule weblogic_module /home/Oracle/Middleware/wlserver_10.3/server/plugin/linux/x86_64/mod_wl_22.so
<IfModule mod_weblogic.c>
<Location /app1>
WebLogicHost serverA
WebLogicPort 7001
SetHandler weblogic-handler
</Location>
<Location /app2>
WebLogicHost serverB
WebLogicPort 8001
SetHandler weblogic-handler
</Location>
</IfModule>
</VirtualHost>
I use this and works fine.

Resources