Config nginx as reverse proxy server

Nginx is open source web server is very popular. It is very fast and support many function as load balance, visual server bla bla … you can get more information about it at main page https://nginx.org/en/

Today, I focus

  • How to add new domain to nginx.
  • How to setup nginx as reverse proxy to forward request to internal service.

Suppose you have some resources

  • Cloud server with static IP 67.205.175.55 and default domain is test.azstack.com.
  • You have new domain test-api.azstack.com want to add point to  67.205.175.55.
  • Service API written by nodejs and listening on port 9002.

How to do it ?

Suppose nginx is installed on linux system, the steps to perform the configuration are as follows :

Step 1 : Check file config of nginx

find / -name "nginx.conf"

Step 2 : Edit and add content to nginx.conf

vi /etc/nginx/nginx.conf

Add the below content

server {
listen 80;
server_name test-api.azstack.com;
root /usr/share/nginx/html;

location / {
proxy_pass http://127.0.0.1:9002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection ‘upgrade’;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

Step 3 : Try to run service and grant permission

-You can check nginx log

tailf /var/log/nginx/error.log

If you have error permission denied,  you continue check SELinux to view detail log

cat /var/log/audit/audit.log | grep nginx | grep denied

The problem is resolved by enable httpd_can_network_connect

setsebool -P httpd_can_network_connect 1

Step 4 : Restart nginx and recheck to make sure nginx work is ok.

Thank you !

 

 

 

Leave a comment