NGINX api routes 404 not found - nginx

NGINX doesn't see the routes, giving 404 NOT FOUND.
The website is located in /usr/share/nginx/html/website folder and accessed via http://localhost.
The route I'm accessing in the application is localhost/api/public/languages.
Here is my NGINX:
root /usr/share/nginx/html/website;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
error_log /var/log/php-fpm/www-error.log;
}
How to access the routes?

Related

ZF3 on nginx only working for parent route

I have two apps. One is simple php file and the other is a zend framework app. I am using Nginx. It is working for simple php app which is accessible via example.com/.
For the ZF3 app, nginx is working only for parent route and none of the child routes are working. The parent route is accessible via example.com/products. I want nginx to handle route actions such as example.com/products/add, example.com/products/view which are already defined in module config.
nginx config
server {
listen *:80;
server_name example.com;
index index.html index.htm index.php;
access_log /var/log/nginx/simpleapp.access.log combined;
error_log /var/log/nginx/simpleapp.error.log;
root /var/www/simpleapp;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ^~ /products {
alias /var/www/ecom/public;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
nginx log file
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
What works:
example.com/
example.com/products
What doesn't work:
example.com/products/add
example.com/products/view
example.com/products/categories
Do you really need two different declarations for very same php processing? I would stick with one and only differentiate the root using alias on /product for example:
server {
listen *:80;
server_name example.com;
index index.html index.htm index.php;
access_log /var/log/nginx/simpleapp.access.log combined;
error_log /var/log/nginx/simpleapp.error.log;
root /var/www/simpleapp;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /products {
alias /var/www/ecom/public;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

Conditionally prepend path to url in NGINX

I have this directory structure for my projects
/var/www
/project-a
/data <-- configuration and user files/data
/app <-- all the code is in sub-dirs in here
/pm <-- a home brew project management app
/.pmdata <-- data used by pm
My goal is to configure NGINX so I can access the project itself through
http://project-a.dev/ and the project management with http://project-a.dev/pm/.
In other words, I want the second url preserved as is, but if the url does not point to /pm/* it should be re-written to have the missing /app prepended to it.
I have tried the following configuration but http://project-a.dev/pm/ results in 404 and http://project-a.dev/ first redirects to http://project-a.dev/app/ and then gives 404.
What am I doing wrong?
server {
listen 127.0.0.1:80;
root /var/www/project-a;
index index.php index.html index.htm;
server_name project-a.dev;
location / {
try_files $uri $uri/app $uri/app/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Alternatively you could append /app to the root value for all URIs that do not begin with /pm. For example:
server {
...
root /var/www/project-a/app;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location ^~ /pm {
root /var/www/project-a;
try_files $uri $uri/ =404;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
The nested location block for location ~ \.php$ executes PHP files within the /pm hierarchy. The ^~ modifier is necessary to avoid the other location ~ \.php$ block taking control. See this document for details.

nginx configuration for Symfony and WordPress running alongside each other

I have a Symfony site at /var/www/mysite/symfony and a WordPress blog at /var/www/mysite/wordpress. How can I direct mysite.com/blog to WordPress and all other requests to Symfony? Here's what I've tried so far:
server {
server_name mysite.com;
root /var/www/mysite/symfony/web;
location / {
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
location ~ ^/blog {
root /var/www/mysite/wordpress;
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
}
access_log /var/log/nginx/mysite-access.log;
error_log /var/log/nginx/mysite-error.log;
}
With this configuration I get a 'File not found.' error when I visit mysite.com/blog. My nginx log file shows FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream. I know it's not an issue with my php-fpm set up as Symfony runs fine at my site root.
Where am I going wrong?
I found a specific rule for installing wordpress in a subfolder:
# Replace all occurrences of [subfolder] with the folder where your WordPress install is located
# The WordPress rewrite
location /[subfolder] {
try_files $uri $uri/ /[subfolder]/index.php?$args;
}
# The default MODX rewrite</h1>
location / {
try_files $uri $uri/ #modx-rewrite;
}
Original post here: Web Rules: Nginx Rewrites, Redirects and other Cloud Configs | MODX Cloud

Nginx location for PHP files that do not exist

I have config(minimal):
server {
listen test.local:80;
server_name test.local;
server_name_in_redirect off;
location / {
root /data/www/test/public;
try_files $uri $uri/ /index.php?route=$uri&$args;
index index.html index.php;
}
location ~ \.php$ {
root /data/www/test/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/test/public$fastcgi_script_name;
fastcgi_param ...
...
}
}
Thats work file for urls /help/ or /contacts/ etc… (all redirect to index.php with get variables).
But if url, for example, /help.php or contacts.php, and this files not exists, I have output:
File not found.
How update my Nginx config? I need URLs, for example:
/help.php => /index.php?route=/help.php
/contacts.php?foo=bar... => /index.php?route=/contacts.php&args=…
Here is simple config for 404 error page . Its custom 404 page.
error_page 404 /404.php;
location /404.php {
root /var/www/public_html;
allow all;
}
Last part
Important line is ========== try_files $uri =404;
location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/public_html$fastcgi_script_name;
include fastcgi_params;
}

nginx Wordpress URL rewrite

I've just installed nginx 1.0.8 and php-fpm and for the last 30 minutes I'm trying to rewrite the URL for Wordpress.
Here is what the Wordpress URL should look like:
http://localhost/website/blog/2011/10/sample-post/
I've looked at this tutorial: http://wiki.nginx.org/WordPress + many other on the web but every time I receive an 404 error (sometimes 403).
Here is what I did in my configuration file:
location /website/blog {
try_files $uri $uri/ /website/blog/index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(/website/blog)(/.*)$;
include fastcgi_params;
error_page 404 /404.html;
}
With this configuration I'm receiving "403 forbidden" status.
What am I missing?
Could be the permission of the root site folder
This is an example that i use to wordpress
server {
listen 80;
server_name www.mysite.com mysite.com;
root /srv/www/mysite.com/public_html;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
include /srv/www/mysite.com/public_html/*.conf;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Did you try restarting nginx after you have saved your config?
Also, check out my nginx/WordPress setup guide here:
http://themesforge.com/featured/high-performance-wordpress-part-3/

Resources