I'm trying to access $_POST and it's empty.
If I try to access it on the root (ie. localhost), it WORKS.
If I try to access in a different folder (ie. localhost/foo), it DOESN'T WORK.
Here is my config file:
server {
listen 80;
root /var/www;
index index.php index.html index.htm;
server_name localhost;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Here is a sample RAW request:
POST /dev/post-test HTTP/1.1
Host: localhost
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="action"
store
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="id"
4315251
----WebKitFormBoundaryE19zNvXGzXaLvS5C
What is the problem?
Thanks.
The key on this differnet behavier is the trailing slash in the route.
For nginx the following are two different requests:
http://localhost/foo
http://localhost/foo/
The first one result in an internal 301 redirect (to index.php into that folder), where the $_POST values are lost.
More about this behavior and solution to prevent it, read the answers below:
Nginx causes 301 redirect if there's no trailing slash
I had same issue - empty $_POST variable.
And found that this happens if a request is sent with this header and the destionation is http instead of https:
Upgrade-Insecure-Requests: 1
In my case I was sending requests from my Unity game to it's server. And it turned out Unity adds that header to all requests sent from the game.
So I had to enable SSL for the server domain and switch from http to https. Once I did that - it worked.
Related
I am using nginx with PHP and Vue. I`m struggling with CORS error like in a title. I can make normal Fetch requests, however when i want to make some application error from backend (for example, when someone want to create a account, and login is already taken). Im getting Error like in title:
Fetch failed and latern in Network Tab in Dev Tools:
CORS: Preflight Missing Allow Origin Header
Here is my nginx.conf:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
server_tokens off;
root /app/;
index index.php;
add_header "Access-Control-Allow-Origin" *;
add_header "Access-Control-Allow-Headers" *;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Way i try to send error:
header("HTTP/1.0 460 Aplication Error");
header("Content-Type: application/json");
json_encode($error);
Am i trying to do in proper way at all?
PS Update, i cant change this header, when using calling it by fetch from Vue CLI Server, when i call it directly for example in browser it works correctly
server {
listen 80;
server_name xx.cn;
index index.php index.html index.htm;
root /data/www_deploy/xx/backend/web;
location ~* /\. {
deny all;
}
location / {
try_files $uri /index.php?$args;
}
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
client_max_body_size 512m;
}
nginx error show that client closed connection while waiting for request, client: x.x.x.x, server: 0.0.0.0:80 when visit domain via client browser
error show that
Connecting to xx.cn (xx.cn)|x.x.x.x|:80... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2019-09-13 19:48:18 ERROR 500: Internal Server Error.
on the server via wget xx.cn
I wonder how to deal it?
This often happens if the index.php (or any other script) you are calling does not exit correctly, for example throwing an exception.
Have a look at the error.log
I am completely new to nginx .
I have project based on angular js which have a index.html, and on a certain event i perform a angular http request to x.php file and fetches the response from it.
Its running perfect on my local system and a apache based private hosting server.
I created a free tier ec2 instance and started a centos based linux instance on which i hosted the code and installed nginx .
Here is my nginx config
server {
listen 80;
server_name mydomain.co.in www.mydomain.co.in;
location / {
root /var/www/html/indm;
index index.php index.html index.htm;
try_files $uri/ $uri /index.php?$query_string =404;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?q=$uri&$args =404;
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;
add_header Access-Control-Allow-Origin *;
proxy_set_header Access-Control-Allow-Origin $http_origin;
}
error_page 500 502 503 504 /50x.html;
At first it was giving me 500 gateway error when the http request was performed on that php file. I check in the XHR request.Here is the error in nginx error.log
[error] 17193#0: *2 rewrite or internal redirection cycle while internally redirecting to "/index.php", client: 42.111.38.254
I searched a bit and changed appended a "=404" to try uri statement. But now it redirect to a 404 . I want to run that php file.
the server
Nginx server is running
php fpm is also running
Please help
The location ~ \.php$ block has no root. Move the root statement into the server block so that the same value is inherited by both location blocks.
For example:
server {
...
root /var/www/html/indm;
location / {
...
}
location ~ \.php$ {
...
}
...
}
I have an issue configuring Nginx correctly to work with Slim POST route.
In a post here on StackOverflow it was said that it is not a good practice to rewrite POST request.
Post: nginx rewrite post data
The idea is to have http://someurl.com/scan which is a POST route in index.php and allow only POST requests on the /scan and deny everything else.
index.php:
$app->post('/scan', function(ServerRequestInterface $request, ResponseInterface $response) { ... }
My Nginx conf looks like this:
server {
listen 80;
server_name someurl.com;
index index.php;
root /var/www/maxime/public;
error_log /var/log/nginx/max.err
access_log /var/log/nginx/max.log
location / {
deny all;
}
location = /scan {
limit_except POST {
deny all;
}
rewrite ^/scan$ /index.php last;
}
location ~ index\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
fastcgi_pass php_pool;
fastcgi_keep_conn on;
}
}
To me this conf tells me that if a request is done on /scan and it is a POST request then we "redirect" everything to /index.php. Then the index.php location block comes into action which passes everything to the index.php or am I mistaking somewhere?
Currently with this setup I get this error in Nginx:
*100778 readv() failed (104: Connection re
set by peer) while reading upstream client: 123.123.123.123, server: someurl.com,
request: "POST /scan HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "someurl.com"
The scenario is that I'd like to use Wordpress as a backend API provider for our Ember.js frontend app.
The Ember.js frontend needs to be served from the root, and the Wordpress instance ideally would be reachable by going to a subdirectory. So for example on localhost it would be http://localhost and http://localhost/wordpress
On the disk the two are deployed in /srv/http/ember and /srv/http/wordpress respectively.
I was trying to assemble the configuration going by the example on the Nginx site:
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
The config:
http {
upstream php {
server unix:/run/php-fpm/php-fpm.sock;
}
server {
listen 80;
server_name localhost;
root /srv/http/ember;
index index.html;
try_files $uri $uri/ /index.html?/$request_uri;
location /wordpress {
root /srv/http/wordpress;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
}
}
However this is obviously not the correct solution.
Upon trying to access the address http://localhost/wordpress/index.php I get the following in the logs:
2016/05/01 17:50:14 [error] 4332#4332: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /wordpress/index.php HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/php-fpm.sock:", host: "localhost"
The recipe isn't clear about where to put the root directive for the location of wordpress. I also tried with adding index index.php, which doesn't help either.
(Serving the Ember app works fine.)
From your question it seems that the location ~ \.php$ block is used by WordPress alone. However, it needs a root of /srv/http in order to find the script files for URIs beginning with /wordpress under the local path /srv/http/wordpress.
As there are two locations which both use the same WordPress root, it is possibly cleaner to make /srv/http the default (that is, inherited from the server block) and move root /srv/http/ember; into a separate location / block.
server {
listen 80;
server_name localhost;
root /srv/http;
location / {
root /srv/http/ember;
index index.html;
try_files $uri $uri/ /index.html?/$request_uri;
}
location /wordpress {
index index.php;
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php;
}
}
Notice that the default URI in location /wordpress is /wordpress/index.php and not /index.php as you originally had.
I have explicitly set SCRIPT_FILENAME as it may or may not appear in your fastcgi.conf file.
fastcgi_split_path_info has been removed as it is unnecessary in your specific case, and I think it would actually break WordPress the way you had it.