Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx reverse proxy setting

I am totally new to Nginx and need your help. Basically I have a single server with single IP address, but I want to host two different web application within the server with different domain name. So, basically, for each domain name, I want it to redirect to different port number. I tried below and got an error

[root@mysvr nginx]# nginx -t -c /etc/nginx/nginx.conf
nginx: [emerg] "proxy_pass" directive is not allowed here in /etc/nginx/nginx.conf:41
nginx: configuration file /etc/nginx/nginx.conf test failed

Following is the Nginx setting. Line 41 is where the proxy_pass is.

server {
  listen 80;
  server_name server1.com www.server1.com;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://127.0.0.1:1003;
}

server {  
  listen 80;
  server_name server2.com www.server2.com;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://192.168.1.1:1004;
}

Thank you!

like image 545
Gon Avatar asked Mar 19 '16 00:03

Gon


People also ask

Why use Nginx reverse proxy?

Security and anonymity – By intercepting requests headed for your backend servers, a reverse proxy server protects their identities and acts as an additional defense against security attacks.

Is Nginx a proxy or reverse proxy?

NGINX Plus and NGINX are the best-in-class reverse proxy and load balancing solutions used by high-traffic websites such as Dropbox, Netflix, and Zynga.

What is Nginx reverse proxy?

Nginx Reverse proxy helps create a balanced load among several back-end servers and provides caching for a slower back-end server Nginx does not require setting up a new process for each web request from the client. Rather, the default configuration is to comprise one work process per CPU

What is the best reverse proxy for a website?

The Most Popular Reverse Proxies. 1 Nginx. Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it’s also one of the most ... 2 Varnish. 3 Apache Traffic Server. 4 HAProxy.

How do I change the route of a Nginx proxy?

The proxy_pass command directs all traffic on port 80 to http://my_server. Just change http://my_server to the location of your choice, and Nginx will intercept client requests and route them to the location you specify. Once you’ve finished, save the file and exit.

What is a proxy pass in Nginx?

In the above command, the considerable point is the proxy pass is allowing the requests coming through the Nginx reverse proxy to pass along to 192.x.x.2:80, which is Apache remote socket. Thus, both the web servers – Nginx and Apache shares the content.


1 Answers

If you check the docs for proxy_pass, proxy_pass needs to be in a location, if in location or limit_except block. You have it in a server block.

Try replacing your usage of proxy_pass with

location / {
    proxy_pass ...
}
like image 51
Kyle Avatar answered Oct 09 '22 08:10

Kyle