Nginx Regex Directing Specific Requests For Custom Blog - php

BACKGROUND
Our website has a blog subsection that runs off a simple custom-made CMS. I'm using Nginx as a webserver, PHP as the back-end language.
The blog index is /news, which contains dynamic links to each post. Clicking one of these links directs the user to /blog/post-title. I'm using Nginx to intercept any request to the post template page located in the /blog folder.
Here's the basic flow
1. User clicks blog with link to /blog/post-title
2. Nginx catches this request and serves it to /blog/index, which is our template
3. The post is displayed with the url https://www.domain.com/blog/post-title
My Problem
I would like to change the blog index from /news to /blog, but doing so causes anchors to direct to the file /blog rather than the folder /blog/.
Here is the current Nginx configuration
location /blog/ {
try_files $uri $uri/ /blog/index.php?$args;
}
TL;DR
I need nginx to catch and direct requests directed toward the folder /blog/ while ignoring the file /blog.php

This is a long shot as I'm not sure how the files are layout on your server but give this a try.
location ~ ^/blog/(.*)$ {
try_files $uri $uri/ /blog/index.php?$1;
}

Related

How to use .htaccess to switch content to display under maintenance message

I want to have /blog directory on my application to have an instance of WordPress installed there. Currently /blog does takes me there, but I do want to know and have control, instead of directory how to read from routes.php.
There can be many use cases for it as blog directory is under maintenance or its down, in that case a simple change so it starts reading /blog from routes.php.
I am not much expert of '.htaccess' file but can we do from it?
if your CI application use the default routes configuration, then /blog will make CI try to load blog controller and index action, if it can't find them. you'll get 404 page not found error.
try to config the /blog in your web server configuration. add /blog block then apply wordpress server config there. make sure that request will find /blog location block before the / location block.
in nginx:
location /blog {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
# then your ci / location
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}

NGINX - Blog (index page) within site gets redirected through proxy correctly, but all sub-sites do not have the same redirect

NGINX noob here, so please pardon my ignorance; I have a server running a DJANGO site, and WordPress blog in a Docker container within the same server; This is the way I have currently set up my NGINX configuration file:
server {
listen 80;
server_name 123.45.67.89;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/johndoe/djangosite;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/johndoe/djangosite/djangosite.sock;
}
location /blog {
rewrite ^/blog(.*) /$1 break;
proxy_pass "http://127.0.0.1:8001";
}
}
So using this NGINX configuration, accessing my server 123.45.67.89, successfully brings up my Django site, and I'm able to use it to its full extent; accessing 123.45.67.89/blog, takes me to my WordPress blog page (the blog index); at this point, clicking anything more within the WordPress blog (like a blog post, or the WP Admin site, or anything else related to the WordPress blog), will change the URL to: 123.45.67.89:8001/wp-login.php instead of 123.45.67.89/blog/wp-login.php.
How can NGINX be configured to use the blog url path, for everything related to the WordPress sub-site, and not just the WordPress index page? I want the port 8001, that keeps showing up in all WordPress URLs to be instead replaced with "/blog/", like it currently does on the blog index page? Is that at all possible?
Any help would be greatly appreciated! Thank you!

Redirect /download URI to subdomain on same domain

I finished developed an app that features a downloading system that is hosted with NGINX at:
http://dashboard.myapp.com
The URL for downloads is:
http://dashboard.myapp.com/download/file-slug
This page is a regular PHP page that will require some user input and then PHP handles the actual file download, it's not the direct path for the file.
Since these download URLs will be made publicly available, I want to ditch that dashboard subdomain.
The default domain (myapp.com) is already working with a wordpress setup with this:
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
Is there an easy way to get the:
http://myapp.com/download/file-slug
to act as if:
http://dashboard.myapp.com/download/file-slug
was accessed, without actually redirecting?
Try this - Place in your server block for myapp.com, anywhere outside another location block. Set the root to the same root as the dashboard subdomain (if on the same server). The script would see itself as being hosed at myapp.com instead of dashboard.myapp.com, but it should retain the remainder of the framework rules. If this doesn't work, try the next option.
location /download/file-slug {
root /path/folder;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
Another option is to proxy through Nginx. This option actually runs the script on the current location, accessing it like a client would through dashboard.myapp.com. See proxy_pass documentation on Nginx.org.
location /download/file-slug { proxy_pass http://dashboard.myapp.com/download/file-slug; }
I was able to work it out with Nginx only.
Inside the myapp.com config file I added:
location ~ /download/(.*) {
resolver 8.8.8.8;
proxy_pass http://dashboard.myapp.com/download/$1;
}
The resolver 8.8.8.8 is actually using Google DNS. Without this line I was getting a "no resolver defined to resolve" error.

Nginx: Redirect all non-existing requests to index.php

I'm in the midst of migrating over to Nginx, from Apache.
I'm currently using a custom content management solution that utilizes the SERVER['request_uri'] to handle routing.
What I am trying to do is redirect all non-existing files & directory requests to /index.php, and not update the clients uri. However, when a file does exist, I want to return that instead.
An example url would be:
localhost/content/page/1 <- Should populate $_SERVER['request_uri'] to be /content/page/1
Or
localhost/public/script/exists.js <- Should be returned as an actual file.
You need to add a location / block or update your current location / block in your nginx vhost file.
This will redirect all request to the index.php if the file or directory is not found:
location / {
try_files $uri $uri/ /index.php;
}
This goes inside your server directive, for more information visit http://wiki.nginx.org/HttpCoreModule
After you modify your vhost file you need to restart nginx
Note: The try_files directive for server blocks was added in 0.7.44

PHP front controller in nginx

I have a wiki that hosts user-generated content with URLs like /wiki/view/pagename and /wiki/modify/pagename. I'm using an nginx configuration that goes something like:
location /wiki/ {
try_files $uri $uri/ /wiki/index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
#fastcgi stuff...
}
It's been working great and as far as i can tell, this is the recommended approach. However, today, a user created a page named "whatever.php", so it needs the URLs /wiki/view/whatever.php to be redirected to my /wiki/index.php... but it gets caught in the second location block and returns a 404 to the user-agent.
Does anyone have any suggestions? Can i add an extra location block to rewrite *.php to the main script somewhere in such a way that won't affect actually routing pages? I still want to use nginx to serve static content inside the /wiki/ directory and to preserve the behaviour of everything outside this directory.
Repost of this dead forum thread

Categories