Woocommerce REST API - Nginx - Error 404 Not Found - rest

I'm trying to use the Woocommerce (v 3.5.4) Rest Api on my VPS (debian 9, Nginx).
Everything works well in my local machine (windows 10, XAMPP).
wpbop/ is the folder (var/www/wpbop/) where the wordpress files are stored.
The next basic URL in a browser should send the endpoints of the API (no need of athentication for this first step) :
http://my-public-ip/wpbop/wp-json/wc/v3
Or a curl in command line
curl http://127.0.0.1/wpbop/wp-json/wc/v3
in both cases, i get error 404 Not Found.
I can acces to the blog / admin blog without any problems ( http://my-public-ip/wpbop )
My permalinks are set on "Postname" in wordpress admin panel, this is recommanded by many people in same case.
EDIT - SOLUTION :
Since my Wordpress installation is in a sub-domain,
try_files $uri $uri/ /index.php$is_args$args;
can't find index.php. Just change this line by :
try_files $uri $uri/ /wpbop/index.php$is_args$args;
and it works !
Perhaps problem is coming from my Nginx conf file ?
server {
server_name localhost;
listen 80;
root /var/www;
location /wpbop {
index index.php;
access_log /var/log/nginx/blog.access.log;
error_log /var/log/nginx/blog.error.log;
try_files $uri $uri/ /index.php$is_args$args;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:7000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
}
I tried many things without any results, and I'm stuck for several days. Can someone help me ?
Thanks for reading me.

This case need a simple fix in the NGINX configuration file. This is related to the path of my wordpress installation.
Since my Wordpress installation is in a sub-domain,
try_files $uri $uri/ /index.php$is_args$args;
--> can't find index.php. Just change this line by :
try_files $uri $uri/ /wpbop/index.php$is_args$args;

when you get 404 code. try to access http://yoursite/?rest_route=/wp/v2/posts
Official documents https://developer.wordpress.org/rest-api/key-concepts

Move root /var/www/; by one level up (to server context). It is not being inherited.

Related

nagvis & nginx configuration: url redirect without location name

i'm trying to make nagvis (http://www.nagvis.org/) work with nginx. Unfortunately i can't manage to do it ...
i have already a server with several other location and i wanted to add a new one for nagvis. here is one of my tries :
server{
listen 443 ssl;
root /apps/www/admin;
index index.php;
ssl_certificate /apps/ssl/admin.chained.crt;
ssl_certificate_key /apps/ssl/admin.key;
server_name admin admin.mycompany.fr;
#auth ldap through pam
auth_pam "Admin Zone";
auth_pam_service_name "admin_nginx";
location ^~ / {
index index.php;
include /apps/etc/nginx/php.conf;
location ^~ /nagvis {
root /usr/share/nagvis/share;
index index.php;
include /apps/etc/nginx/php.conf;
try_files $uri $uri/ /index.php;
}
location ^~ /phpmyadmin {
root /usr/share;
index index.php;
include /apps/etc/nginx/php.conf;
try_files $uri $uri/ /index.php;
when i tried "https://admin.soc.mycompany.fr/nagvis/index.php" with this configuration i get :
==> /var/log/nginx/error.log <==
2015/06/30 11:14:11 [error] 10568#10568: *185 FastCGI sent in stderr: "Unable to open primary script: /usr/share/nagvis/share/nagvis/frontend/nagvis-js/index.php (No such file or directory)" while reading response header from upstream, client: 172.20.0.5, server: admin, request: "GET /nagvis/frontend/nagvis-js/index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "admin.mycompany.fr"
==> /var/log/nginx/access.log <==
172.20.0.5 - lagarjoc [30/Jun/2015:11:14:11 +0200] "GET /nagvis/frontend/nagvis-js/index.php HTTP/1.1" 404 56 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/41.0.2272.76 Chrome/41.0.2272.76 Safari/537.36"
the first index.php in /usr/share/nagvis/share is a redirect to "/frontend/nagvis-js/index.php". It's working well and being redirect to https://admin.mycompany.fr/nagvis/frontend/nagvis-js/index.php
but nginx itself is looking for the file in " /usr/share/nagvis/share/nagvis/frontend/nagvis-js/index.php" instead of
"/usr/share/nagvis/share/frontend/nagvis-js/index.php" (the location is to much).
i hope i was clear enough and that you would be able to help me.
thanks in advance, i'm stuck :(
well, i found a "hack" to make it work.
i simply added a symbolic link like this :
ln -s /usr/share/nagvis/share /usr/share/nagvis/share/nagvis
i would have like to know if i could avoid the issue directly through nginx configuration but at least this work find for the moment!
hope it will help someone too.

Redirect all traffic to SSL except one location

I have an nginx server running serving my website. All the connections are redirected to SSL for security reasons.
However, I was desperately looking how to exclude one location from this redirect. I already tried rewrite, redirect, proxy_pass etc. but it doesn't seem to work.
I don't want to (301 or 302) redirect my site, I only want SSL to be optional. The location various types of files (js, php, html).
For example
server {
listen 80;
server_name example.com
root /var/www/example;
location /unsafe {
try_files $uri $uri/ /index.php;
}
location / {
rewrite ^ https://$server_name$request_uri? permanent;
}
# other rules...
}
server {
listen 443;
server_name example.com
root /var/www/example;
location / {
try_files $uri $uri/ /index.php;
}
# other rules...
}
does not work.
I also tried using redirect or rewrite instead of try_files, but no luck at all. The thing is, I don't want to the traffic to be redirected, rewritten or proxy'd, I only want nginx to pass all requests on example.com/unsafe
All I get is a bunch of 404's ands 502's.
What am I doing wrong?
Cheers
You should have separate server blocks for the normal http connections (on port 80) and the https SSL connections (on port 443).
server {
listen 80;
server_name your-domain.com
root /var/www/;
location /unsafe {
try_files $uri $uri/ /index.php;
}
# your other rules...
}
server {
listen 443;
server_name your-domain.com
root /var/www/;
location / {
try_files $uri $uri/ /index.php;
}
# your other rules...
}
Amended code:
If you want all files on your site to use https connections (SSL, port 443) EXCEPT those files inside the /unsafe directory, this is what your server blocks should look like:
# This server block handles all requests on port 80 and serves only files inside
# the /unsafe directory. Everything else is redirected to an SSL connection on
# port 443.
server {
listen 80;
server_name your-domain.com
root /var/www/;
# only serve requests to files in the /unsafe directory
location /unsafe {
try_files $uri $uri/ =404;
}
# all other locations redirect to https connection
location / {
return 301 https://your-domain.com$request_uri;
}
# this location block proxies requests for PHP files to
# your fcgi php processor
location ~ /unsafe/.*\.php$ {
try_files $uri =404;
# your fcgi rules here...
}
# your other rules...
}
# This server block handles all SSL (port 443) connections.
server {
listen 443;
server_name your-domain.com
root /var/www/;
location / {
try_files $uri $uri/ =404;
}
# this location block proxies requests for PHP files to
# your fcgi php processor
location ~ \.php$ {
try_files $uri =404;
# your fcgi rules here...
}
# your other rules...
}

facebook 502 nginx + fpm

I'm unable to post any pictures or other media in facebook. I have used the debuger (http://developers.facebook.com/tools/debug) and I always get:
Scrape Information
Response Code: 502
I use nginx 1.2.0 with php-fpm with sock not port (9000)
My errror log does not show any error. The access log
69.171.237.14 - - [23/Mar/2013:19:00:29 +0100] "GET /video/X1KAW64412WH1OO/5123 HTTP/1.1" 200 11715 "-" "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" "-"
Currently I have disabled the IPtables. php.ini and most of the timeouts are set to 3600
nginx.conf part:
location ~ \.php$ {
root /home/blabla/www;
# fastcgi_pass 127.0.0.1:9000;
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/blabla/blabla/$fastcgi_script_name;
# fastcgi_param REQUEST_URI $request_uri;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
access_log logs/access._php.log main;
fastcgi_send_timeout 5m;
fastcgi_read_timeout 5m;
fastcgi_connect_timeout 5m; }
I have tested using cloudflare services and it works great, but when I only point to my server it stop. It happen to all my other websites located to this machine so it must be webserver configuration prolem I guess. I use centos x64
I was running into the same issue.
My nginx logs indicated that a 200 response was being served, and requests in the browser showed a 200 response. Facebook was insistent that it was a 502 error.
It turns out that '502' can mean either 'Your server returned a 502' or 'I ran into a difficulty parsing the response'.
In my case, I had a non-compliant HTTP header (it conained a single question mark), which was causing Facebook to reject the response as being invalid. Removing this header fixed the issue.
Facebook by default use ipv6 address if available To Solve this problem you have to enable iPv6 in a Ningx config file for each virtual host (if many sites hosted) to listen Any IPv6 address at port 80.
This will solve the issue with Facebook opengraph.

dotCloud nginx.conf: how to get the "index index.php" directive to work?

I got auth to work by "pushing" an nginx.conf file in the application directory, so I know the file works, but /app will not trigger /app/php.index. I can't get nginx working on my vista laptop with php and I can't edit the /etc/nginx/nginx.conf in the dotCloud instance as dotCloud makes life difficult by not giving root.
(Note that the .htpasswd is relative to the ngnix.conf file location, nice).
server {
location / {
index index.php;
}
location /admin {
auth_basic "enter password";
auth_basic_user_file .htpasswd;
index index.php;
}
}
You may need an nginx directive to map requests to your dynamic content. This controller can can then route them appropriately.
try_files $uri $uri/ /index.php;
For an example project, see the CakePHP tutorial.

nginx fastcgi configuration for CGI::Application app

I am trying to get a C::A app work in nginx fastcgi environment (debian 6.0) and using spawn-fcgi.
C::A route is configured using $self->mode_param( path_info=> 1, param => 'rm' );
the problem is that whatever C::A app urls (example.com/cities, example.com/profile/99 etc ) I am requesting, it always displays the homepage which is what the example.com/index.pl does.
my nginx setup is
server {
listen 80;
server_name example.com;
root /var/www/example.com/htdocs;
index index.pl index.html;
location / {
try_files $uri $uri/ /index.pl;
}
location ~ .*\.pl$ {
include fastcgi_params; # this is the stock fastcgi_params file supplied in debian 6.0
fastcgi_index index.pl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PERL5LIB "/var/www/example.com/lib";
fastcgi_param CGIAPP_CONFIG_FILE "/var/www/example.com/conf/my.conf";
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
}
I have successfully setup few php apps in similar fashion.
in this case, however, I suspect that I am not passing essential fastcgi_param down to C::A which is required by it.
what's your thoughts?
I maintain CGI::Application and also use Nginx. I have not done the same thing, but I would try this:
fastcgi_split_path_info ^(/index.pl)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
This is supposed to capture and forward the PATH_INFO that you need.
References:
Offical Nginx FastCGI docs
Related blog post with example This is not for CGI::App, but I think it's close enough to be helpful.
I ended up solving the problem with a workaround in my C::A app. And I am documenting it here.
So I didn't managed to have nginx pass along the PATH_INFO down to my C::A app. To work around this, I set the PATH_INFO with the value of REQUEST_URI in my C::A app so it picks up the correct runmode.
Also, nginx isn't passing QUERY_STRING either so I had to append $query_string to the catch all route in order to pass along QUERY_STRING down as well.
my nginx config ends up like this:
server {
listen 80;
server_name example.com;
root /var/www/example.com/htdocs;
index index.pl index.html;
location / {
try_files $uri $uri/ /index.pl?$query_string;
}
location ~ .*\.pl$ {
include fastcgi_params; # this is the stock fastcgi_params file supplied in debian 6.0
fastcgi_index index.pl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PERL5LIB "/var/www/example.com/lib";
fastcgi_param CGIAPP_CONFIG_FILE "/var/www/example.com/conf/my.conf";
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
}