0

Heyo, front end guy moving website from shared hosting to SPA droplet. I have a Headless CMS in /build, a Node SendGrid Mailserver in /mail, and am trying to have some html and php in /wp-content/themes/webdev/projects/trackjob/point_card/. (it's a php proxy) The reason is an old client used an iframe to this url from my old site and its just best to copy it over.

The card_api_js_v2.html will need to make an AJAX request to a php file in the same parent directory.

I am just getting more familiar with nginx. Advice?

Currently, I have this...

location / {
   root /var/www/build;
   try_files $uri $uri/ /;
}

location /mail {
   root /var/www/server;
   proxy_pass http://localhost:3000;
   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;
}

// DOESNT WORK ??? 
location /wp-content/themes/webdev/projects/trackjob/point_card/ {
   root /var/www/wp-content/themes/webdev/projects/trackjob/point_card;  
   try_files $uri $uri/ /card_api_js_v2.html;
}

2 Answers 2

2

You are misunderstanding on how the root directive works. When you use this location block

location /wp-content/themes/webdev/projects/trackjob/point_card/ {
   root /var/www/wp-content/themes/webdev/projects/trackjob/point_card;
   try_files $uri $uri/ /card_api_js_v2.html;
}

and got a /wp-content/themes/webdev/projects/trackjob/point_card/card_api_js_v2.html incoming request, nginx concatenates the $document_root (which is /var/www/wp-content/themes/webdev/projects/trackjob/point_card) and $uri (which is /wp-content/themes/webdev/projects/trackjob/point_card/card_api_js_v2.html) variables and searches for the file /var/www/wp-content/themes/webdev/projects/trackjob/point_card/wp-content/themes/webdev/projects/trackjob/point_card/card_api_js_v2.html (which is obviously would not be found). This is the main difference between the root and alias nginx directives. Your location block should be the

location /wp-content/themes/webdev/projects/trackjob/point_card/ {
   root /var/www;  
   try_files $uri $uri/ /wp-content/themes/webdev/projects/trackjob/point_card/card_api_js_v2.html;
}

instead.

Update

If you need to serve the PHP scripts inside this location, change it to

location ^~ /wp-content/themes/webdev/projects/trackjob/point_card/ {
   root /var/www;  
   try_files $uri $uri/ /wp-content/themes/webdev/projects/trackjob/point_card/card_api_js_v2.html;
   location ~ \.php$ {
      include fastcgi.conf;
      fastcgi_param SCRIPT_FILENAME $request_filename;
      # if your php-fpm listens to the UNIX socket (assuming default socket path)
      # fastcgi_pass unix:/var/run/php/php7.4-fpm.sock
      # if your php-fpm listens to the TCP/IP port (assuming default port)
      # fastcgi_pass 127.0.0.1:9000;
   }
}
Sign up to request clarification or add additional context in comments.

9 Comments

OHHHHH.... well thank you let me go try this now.Combined with IVO's answer I think this will do the trick. I am starting to understand this better thank you!
Okay so here is one follow up question I need to route it differently if the request is to a php file. So for example this ajax request needs to work... URL/wp-content/themes/webdev/projects/trackjob/point_card/card_proxy.php?oncard=000000000000000
That ajax request is made from the html card_api_js_v2.html file and it goes to a file called card_proxy.php. Is this just a matcher difference or can I actually put the exact file name in the main location directory and create an additional one?
@MichaelPaccione Check the update to the answer. Do not miss the ^~ after the location keyword or each request for the PHP script will be processed by your primary PHP handler location.
Hmmm I seem to get a 502 error on this. This is the github gist: gist.github.com/mpaccione/c12069cd32de5ac587d70b56283dd722
|
1

In order to actually run the PHP files (rather than sending their content as text to the browser) you will need to use PHP-FPM:

        location ~ \.php(/|$) {
            try_files $uri =444;

            fastcgi_param  QUERY_STRING       $query_string;
            fastcgi_param  REQUEST_METHOD     $request_method;
            fastcgi_param  CONTENT_TYPE       $content_type;
            fastcgi_param  CONTENT_LENGTH     $content_length;
 
            fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO          $fastcgi_path_info;
            fastcgi_param  HTTPS              $https;
            fastcgi_param  REQUEST_URI        $request_uri;
            fastcgi_param  DOCUMENT_URI       $document_uri;
            fastcgi_param  DOCUMENT_ROOT      $document_root;
            fastcgi_param  SERVER_PROTOCOL    $server_protocol;
 
            fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
            fastcgi_param  SERVER_SOFTWARE    custom;
 
            fastcgi_param  REMOTE_ADDR        $remote_addr;
            fastcgi_param  REMOTE_PORT        $remote_port;
            fastcgi_param  SERVER_ADDR        $server_addr;
            fastcgi_param  SERVER_PORT        $server_port;
            fastcgi_param  SERVER_NAME        $server_name;

            fastcgi_pass 127.0.0.1:9000;
            fastcgi_connect_timeout 120;
            fastcgi_send_timeout 600;
            fastcgi_read_timeout 600;
            fastcgi_buffering off;
            fastcgi_request_buffering off;
            fastcgi_buffers 8 128k;
            fastcgi_buffer_size 128k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
            fastcgi_intercept_errors on;
            fastcgi_ignore_client_abort off;
        }

1 Comment

Thank you! Very helpful :) I am wondering if you look back at the original question is there a fix for the location settings that doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.