📜 ⬆️ ⬇️

Setting up nginx to work with apache and tomcat servers using the example of Ubuntu 10.04 Server

There was a desire to put on your nginx server. And then there was a need to leave Apache (nginx can completely replace it). Apache Tomcat is also running on the server, while it is empty, but still there. I will describe a simple example of how to make nginx work as a front-end proxy, listening to port 80 and redirecting requests to other servers, depending on the context. Go…

For a start we will decide on versions. apache2 is pretty fresh in the Ubuntu 10.04 repositories. Recently there appeared the latest version of the tomcat server. But nginx is old, very old, just super-old. Collect the latest version from source? If you want to rebuild the bike - please. The fact is that the highly respected Nikita Kardashin from the nginx forum collects the latest versions of nginx in deb-packages and kindly presents them in his repository. There are packages even for lucid. Let's add its repositories to sources.list. We make ourselves a root and then we do everything from it:

$ sudo su
# echo 'deb repo.dlocal.ru/nginx-repo/ubuntu/nginx8 lucid/' >> ./etc/apt/sources.list
# apt-get update
# apt-get install apache2 nginx


Why am I not installing tomcat6? Yes, because despite the updated version in the repositories, it was not workable. Those. You can bring the server to a working state, but why fuck your brain if you can download, unpack and enjoy? But first, let's configure Apache and NginX. Edit the /etc/apache2/ports.conf file, forcing apache to accept connections only from 127.0.0.1:

NameVirtualHost *:80
Listen 127.0.0.1:80 # apache localhost 80

<ifmodule mod_ssl.c="">
# SSL name based virtual hosts are not yet supported, therefore no
# NameVirtualHost statement here
Listen 127.0.0.1:443 # 443 ssl

')
Now configure the proxy in nginx. edit the / etc / nginx / sites-available / default file:

server {
listen ...:80; # ... ip
server_name www.example.com; # www.example.com dns

access_log /var/log/nginx/access.log;

# localhost 80
location / {
proxy_pass localhost:80/;
proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

# , nginx apache
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js)$ {
root /var/www;
}
}


Restart apache and nginx:

# /etc/init.d/apache2 restart
# /etc/init.d/nginx restart


We now turn to tomcat. Let's take it "installation":

# cd /opt
# wget apache.infocom.ua/tomcat/tomcat-6/v6.0.26/bin/apache-tomcat-6.0.26.tar.gz
# tar -xvzf ./apache-tomcat-6.0.26.tar.gz
# rm ./apache-tomcat-6.0.26.tar.gz
# ln -s ./apache-tomcat-6.0.26 ./tomcat


Next you need to edit its settings. In the file /opt/tomcat/conf/server.xml we find the Connector section and bring it to the following form:

<Connector address="127.0.0.1" port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />


Now tomcat also accepts connections only from the local 127.0.0.1. Next, in the nginx settings in the / etc / nginx / sites-available / default file, we create another location section, for example:

location /tomcat {
proxy_pass localhost:8080/;
proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}


Now at <your_ip> / tomcat will answer tomcat. Thanks for attention.

Source: https://habr.com/ru/post/95677/


All Articles