Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx location path with proxy_pass

I have following problem, i'm trying to put a Django app with an gunicorn server on my VPS running Nginx. My nginx config looks like this:

upstream app_name {
    server unix:/path/to/socket/file.sock fail_timeout=10;
}

server {

   listen 80 default_server;
   listen[::]:80 default_server ipv6only=on;
   root /webapps/;
   server_name my_hostname.com;

   location / {
      proxy_set_header Host $http_host;
}

   location /appname/ {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;

      proxy_pass http://app_name;

}

}

However when i navigate to my_server.com/appname/ i constantly get 404 error. I'm still new to Nginx, could someone point me in the right direction on how to set the proxy_pass for /appname/ path? I should point out that when location for /appname/ is replaced with / the django app is running fine.

like image 575
Konrad Wąsowicz Avatar asked Aug 24 '14 18:08

Konrad Wąsowicz


People also ask

What does proxy_pass do in nginx?

The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.

Where is Nginx location config?

Every NGINX configuration file will be found in the /etc/nginx/ directory, with the main configuration file located in /etc/nginx/nginx. conf . NGINX configuration options are known as “directives”: these are arranged into groups, known interchangeably as blocks or contexts .

How do you check if nginx reverse proxy is working?

To check the status of Nginx, run systemctl status nginx . This command generates some useful information. As this screenshot shows, Nginx is in active (running) status, and the process ID of the Nginx instance is 8539.


1 Answers

You just need a trailing slash for proxy_pass:

proxy_pass http://app_name/;

it helps you to cut the "appname" prefix so the config looks like:

upstream app_name {
    server unix:/path/to/socket/file.sock fail_timeout=10;
}

server {

   listen 80 default_server;
   listen[::]:80 default_server ipv6only=on;
   root /webapps/;
   server_name my_hostname.com;

   location /appname/ {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;

      proxy_pass http://app_name/;

}
like image 138
Anatoly Avatar answered Oct 17 '22 10:10

Anatoly