0

I have 2 folders dist and api_middleware in '/var/www/public_html/website.com/'

dist folder contains my frontend production code. This folder is symblink-ed from /home/user/frontend/dist

api_middleware folder contains codeignitor code, that we use as middleware for frontend to communicate with our erp. This folder is symblink-ed from /home/user/api_middleware.

I want to host both code with nginx. And this is the code that I came up with.

server { listen 443; ssl on; ssl_certificate /etc/ssl/website.com.crt; ssl_certificate_key /etc/ssl/website.com.key; server_name website.com; access_log /var/log/nginx/nginx.vhost.access.log; error_log /var/log/nginx/nginx.vhost.error.log; index index.php index.html index.htm index.nginx-debian.html; location / { root /var/www/public_html/website.com/dist; try_files $uri $uri/ /index.html; } location /api_middleware { root /var/www/public_html/website.com; try_files $uri $uri/ /api_middleware/index.php?/$request_uri; client_max_body_size 100M; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; fastcgi_read_timeout 300; } } 

navigating to website.com works. But the api calls with website.com\api_middleware\<PARAMS> is returning a 404

What am I doing wrong?

PS. Using ubuntu 18.4.

1 Answer 1

0

My Updated working code:

server { listen 443; ssl on; ssl_certificate /etc/ssl/website.com.crt; ssl_certificate_key /etc/ssl/website.com.key; server_name website.com; access_log /var/log/nginx/nginx.vhost.access.log; error_log /var/log/nginx/nginx.vhost.error.log; root /var/www/public_html/website.com/dist; index index.php index.html index.htm index.nginx-debian.html; location /api_middleware { alias /var/www/public_html/website.com/api_middleware/; try_files $uri $uri/ /api_middleware/api_middleware/index.php?/$request_uri; client_max_body_size 100M; } location ~ /api_middleware/.+\.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; fastcgi_read_timeout 300; } } 

The issue was that using multiple root was messing with nginx to access the directory location my second codebase.

I used alias to change the location to search for my 2nd codebase, while using root to set location of my 1st codebase.

Take a look at the line try_files $uri $uri/ /api_middleware/api_middleware/index.php?/$request_uri;. I have added 'api_middleware' twice here, even though the location to index.php file is /var/www/public_html/website.com/api_middleware/index.php . This is needed because of a very old bug in nginx.

finally, to process the php files, I changed the URI where to look for the php files to ~ /api_middleware/.+\.php$.

Reference:

  1. NGINX try_files + alias directives
  2. https://serverfault.com/questions/1035733/nginx-difference-between-root-and-alias
  3. https://serverfault.com/questions/684523/nginx-multiple-roots/684605
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.