View file with question mark in nginx - http

I've got some code that mirrors some pages using wget, and some of the pages to be mirrored are links like "http://example.com/somepage.php?id=1234". wget ends up saving those pages as "somepage.php?id=1234.html". When I try to visit that page, I get a 404.
I've tried adding "autoindex on;" to the config for that directory to make sure that the filename is correct, and I click on the link generated by nginx when I visit the directory that contains that page, and I still get a 404.
How can I get nginx to serve pages with a question mark in the name?

To future readers: visiting somepage.php%3Fid=1234.html worked. (See the comments above.)

Had this issue too, the solution was replacing try_files $uri with try_files $request_uri.
Or you could use both:
try_files $uri $request_uri =404;

Related

How can i use NGINX try_files without changing the browser location for the client?

I am using nginx try_files for Server-Side-Rendering. Since GET-parameters affect the resulting html, i want to use try_files to check if i have a prerendered version with the matching query string (file is saved at /posts/?page=5/index.html)
I have the following try_files statement in place:
try_files $uri/$is_args$args/ $uri $uri/ #ssr;
This works, but the client gets redirected to www.page.com/posts/?page=5 which is including the slash of the subdirectory i have. This results in the browser getting a 404 in the end, because that post does not exist.
How can i use try_files like i did, but without actually redirecting the client? To be clear: I want to serve files from whereever with try_files, but not actually let the client know which file is served, thats none of the browsers business in my case.
Is there a way to achieve this?

How to fix nginx conf for static site?

I don't understand why my nginx.conf file does not work.
This is just supposed to serve a static web site.
Later I want to add a wordpress to /blog.
Currently I want to make the main static site work.
The related conf is this:
location / {
index index.html
root /var/www/html/xlanding;
try_files $uri $uri/ =406;
}
This nginx is dockerized. I log in to the docker container and I can confirm that /var/www/html/xlanding/ exists and there is an index.html file there.
When I visit the url I see a 406 error (just changed from 404 to confirm that block is entered). So the block is matched.
How can I fix this?
Ah, I missed a semicolon after the "index index.html".

NGINX try_files works for all but one url

I have a simple dashboard for my site. Here is the directive:
location /dashboard {
try_files $uri /dashboard/index.php;
}
It works for all items after /dashboard. For example, /users or /pages - all CRUD operations work as expected.
The index.php file at /dashboard is my "controller". It parses the url and includes and runs scripts from there.
For example: /dashboard/group/edit/123456 works as expected and I get the edit page for group number 123456.
But when I post from that page to /dashboard/group/update, it serves /dashboard/group/index.php
So, in the first example, The edit page is loaded and the url at the top of the screen does not change.
In the second example, NGINX is CHANGING the url so my script cannot get the url parts to do the job.
I thought it may have something to do with POST, but I have other forms that use POST without issue.
In addition, or possibly a clue, try_files is returning /dashboard/group/index.php while the directive should return /dashboard/index.php.
Is there another NGINX file that could have so old code in it that is overwriting this domain's config?
I've been at this a few hours and have run out of ideas. Any thoughts?
* One More Clue *
When I BROWSE to /dashboard/group/update, NGINX shows the page as expected. It is only when I POST to that page that NGNIX sends me to /dashboard/group/index.php.
Again, at the very least, it should be sending me to /dashboard/index.php and NOT /dashboard/group/index.php.
You not send all after /dashboard try this:
location /dashboard {
try_files $uri /dashboard/index.php?$uri&$args;
}
OR
location /dashboard {
try_files $uri /dashboard/index.php?$query_string;
}
Nginx docs: https://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
Instead of
location /dashboard {
try_files $uri /dashboard/index.php;
}
Try
location /dashboard {
index index.php; #adding this may work alone
try_files $uri /dashboard/index.php?$uri;
}
I have concluded that I have a cache problem. The location directive works on all items that I have not yet accessed.
So, my configuration - as described - works as it should.
I just have to figure out how to clear my cache ( which in NOT set up in NGINX that I can see!)
Thank you all who helped!

nginx Sinatra head meta refresh 404

For some reason, a page is forwarding to an nginx-served 404 page. The site is working except for this one page. On my landing page, I have the following in the header:
<meta http-equiv="refresh" content="5;URL=/second">
Even hard coding http://mydomain.local/second serves up the same 404.
This is a Sinatra deployment.
get "/second" do
#title = "my Title"
erb :index
end
I am thinking maybe my locations directive in nginx.conf is a bit screwy:
location / {
autoindex off;
try_files $uri $uri/ $uri/$request =404;
index /;
}
Anybody have any insight why Sinatra wouldn't capture the route and just serve up the file? Cheers.
Lets break this down:
autoindex off;
You don't need to specify this, it's off by default.
try_files $uri $uri/ $uri/$request =404;
Try and match requests to files in this order, serve the first one which matches. If none match, return 404. Your request is http://mydomain.local/second, so you are telling Nginx to look for:
webroot/second
webroot/second/
webroot/second/GET /second HTTP/1.1
Return 404
The index directive tells Nginx how to handle requests ending with a slash, you would normally specify files likely to be index files within your project, eg index.php, index.html etc. You don't really use this and try_files together as they serve a similar purpose.
Also worth noting that Nginx adds a trailing slash to any request which doesn't have one and doesn't end with a file extension, so it may also be the case that get "/second" do needs to change to get "/second/" do, as I doubt Sinatra ever sees the request without the trailing slash appended

NGINX Wordpress install in subdirectory wrong script URL

Switching from Apache to NGINX here :)
So I have Site A which sits on the root directory http://test.fake.com/ and works fine. I also have another Wordpress Site (B) where the root is http://test.fake.com/b/
All the front end pages for B load scripts and css like http://test.fake.com/test.fake.com/b/ which of course is incorrect. When I try to goto the Admin like http://test.fake.com/wp-admin/ I receive No input file specified. as the only output, also the URL changes to http://test.fake.com/test.fake.com/b/wp-login.php.
Here is a snippet of my NGINX config where I think the problem lies, please let me know if you need more info and be gentle on a NGINX n00b :P
location /b {
try_files $uri $uri/ /b/index.php$args;
}
location / {
# Set try_files according WP architecture
try_files $uri $uri/ /index.php$args;
}
Here is a snippet of B's wp-config file where I am setting the URLs
define('WP_HOME','http://test.fake.com/b');
define('WP_SITEURL','http:/test.fake.com/b');
I have no idea where the error lies :(
Thank you for viewing!

Resources