Config nginx.conf so that .html extensions aren't displayed - nginx

I'm new to nginx configuration so not sure how to go about this, but any help would be appreciated.
I have a company.com/jobs.html page set up, but would like to point the URL company.com/jobs to the content of company.com/jobs.html while keeping the URL as company.com/jobs
This is what I changed in my nginx.conf file:
location / {
rewrite ^/jobs$ jobs.html last;
}
Unfortunately, I'm running into some trouble and not sure what to try next. Any help is appreciated!

location ~ ^/jobs$ {
try_files $uri.html $uri;
}

Related

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 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!

Nginx load another file at home page

I have an Nginx configuration that's working, but I have a weird situation. The /index.html file isn't what I want to load at / or /index.html. At those locations I want to load /home/index.html. I'm not sure what the best strategy is to configure this. This is the pseudo-config I have so far.
location [/ or /index.html] {
load /home/index.html instead, respond 200 not with a redirect;
}
You might use try_files for this. Something like:
location / {
try_files $uri /home/index.html;
}
Note: This will give you /home/index.html for every bad address too, maybe not what you want.
Edit: Or just symlink /index.html to /home/index.html.

nginx rewrite virtual directory to file

This should be really easy to do but I'm hitting my head on the wall. If I get a request for www.mysite.com/mypath I want to serve the content of www.mysite.com/myotherpath/thisfile.html. How can I do this with an nginx config.
location = /mypath {
try_files /myotherpath/thisfile.html =404;
}
http://nginx.org/r/try_files
http://nginx.org/r/location
Use rewrite directive within proper location block. So for example you have basic location which will handle all requests
location / {
/*your rules here*/
}
You will need to add another block, which will do for you handling of specific path
location /mypath {
rewrite ^/mypath$ /real/path/to/file/thisfile.html;
}
Also for your server to think in that block that thisfile.html is default you can use try thisfile.html directive
It is all well explained on official page Official Nginx RewriteModule page

Please help me understand simple nginx rewrite issue for subdomain robots.txt

For my subdomain I wanted to point to a different robots.txt file. I had hoped the following code would work:
if ($host ~ subdomain) {
rewrite ^/favicon.ico$ /favicon.ico break;
rewrite ^/robots.txt$ /robots.subdomain.txt break;
rewrite ^/([^\/]*)?/?(.*)?$ /index.php?in1=$1&in2=$2&$query_string last;
}
favicon.ico works fine, all other extensions are rewritten to index.php just fine, but so is robots.txt.
I spent [wasted] a lot of time trying to solve it, which I did by adding the following line after the robots.txt rewrite.
rewrite ^/robots.subdomain.txt$ /robots.subdomain.txt break;
Can someone please help me why it only works when I add this line, also any improvements to my config would be welcomed if you see any obvious inefficiencies! Thank you.
This should be what you're looking for:
location / {
rewrite ^/robots.txt$ /robots.$host.txt; # rewrites robots.txt
try_files $uri $uri/ #php; # try_files serves statics and sends everything else
# to your front controller (index.php)
# files such as favicon.ico get served normally
}
location #php {
rewrite ^/([^\/]*)?/?(.*)?$ /index.php?in1=$1&in2=$2 last;
}
The only caveat is that your robots.txt needs to be named after the full host, so in the example above your www.domain.com needs to have a robots.www.domain.com.txt file in the document root. This seems a bit ugly, so I'd do it this way instead:
rewrite ^/robots.txt$ /$host.robots.txt;
and then you name your file www.example.com.robots.txt

Resources