NGINX: main location block overriding sub directory location? - nginx

I've read a moderate amount of the documentation for location blocks, but I dont have much experience with RegEx so I am a bit lost on how to pull off what I am trying to do. The following nginx config will probably explain what I want to do better than I can word it:
server {
server_name example.com www.example.com;
root /var/www/;
index index.php;
location /blog/ {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location / {
try_files $uri #uwsgi;
}
location #uwsgi {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
}
example.com/ is being served by a bottle app through uwsgi, and so all things under this location should be routed to the bottle app and handled there. This is working fine as expected, however I am lost on how to add an 'exception' to the location rule so that example.com/blog, and everything under it ../sub1/sub2 etc. are not directed to the bottle app, but infact handled by wordpress and its PHP magic.
This seems like it should be very simple to set up, but it's proving very difficult to google simple solutions to these sort of problems, as everyone seems to bloat thier 'tutorial' configurations with tons of non-essentials that confuse a beginner.

This may need some tweaks, but you should probably use a nested location block:
server {
server_name example.com www.example.com;
root /var/www;
index index.php;
location /blog/ {
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
location / {
try_files $uri #uwsgi;
}
location #uwsgi {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
}
Notice that the default URI is changed to /blog/index.php which is hopefully where all of your WordPress files are located.

Related

Nginx with wordpress, use reverse proxy as fallback

I am trying to setup nginx to try files in the following order:
1) See if file exists directly on server
2) See if file exists as part of wordpress
3) Fallback to external server if neither 1 or 2 exist.
I have the following setup:
location / {
try_files $uri $uri/ /index.php?q=$uri&$args #proxy;
}
location #proxy {
proxy_pass https://external.website.com;
}
This doesn't work for wordpress, and nginx's try_files documentation is pretty unhelpful. I can see that whichever arg is last is the fallback, so I tried flipping the last two args, and that caused wordpress to work, but then the reverse proxy didn't work.
I think I solved it. Technically this checks the proxy first, but that's okay for what I want to do. If anyone solves the correct way, I will choose them.
location / {
try_files $uri $uri/ #proxy;
}
location #proxy {
proxy_pass https://external.website.com;
proxy_intercept_errors on;
recursive_error_pages on;
error_page 404 = #wordpress;
}
location #wordpress{
try_files $uri /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

How do I remove the file extension from URLs in nginx?

Let's say I have a file named careers.php, how do I serve this file when they click a link that goes to http://example.com/careers without the file extension for both .html and .php files with nginx?
Please note that the solution has to account for query strings. For instance, the URL may be http://example.com/careers?lang=fr.
Also, I'd like the solution to also try subdirectories as well. For instance; if my folder structure on the server is /views/careers.php, I want http://example.com/careers to still serve /views/careers.php.
My current configuration looks like the following:
server {
listen 80 default_server;
root /usr/share/nginx/landing-page;
index index.php index.html;
server_name example.com;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
A common solution uses try_files with a named location. The existing location ~ \.php$ block is used to process .php files. Also avoid any if statements.
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^ $uri.php last;
}
location ~ \.php$ {
try_files $uri =404;
...
}
You can use try_files:
try_files $uri $uri.php $uri/ =404;
I know that this is an old post, but I feel like this was never perfectly explained or explained using the most simple solution.
Here is an example of how I accomplished this for a React Application served using nginx for my Portfolio Website hosted on Digital Ocean.
When simplified it's super easy to accomplish. The location block should look like this.
//Originally my location block was set up like this
location /chatgpt-homepage.html {
root /var/www/myportfoliowebsite.com/html/chatgpt-app-homepage/build;
}
//What needs to happen is you need to remove the .html from your location
//and
//Add try_files $uri $uri.html $uri/ =404; underneath your root section of code
//New Location Block example below
location /chatgpt-homepage {
root /var/www/myportfoliowebsite.com/html/chatgpt-app-homepage/build;
try_files $uri $uri.html $uri/ =404;
}

nginx rewrite sub url

How can I solve this problem: I want to set up nginx conf file to meet below criteria:
http://www.example.com/site1/article/index.php?q=hello-world -> http://www.example.com/site1/article/hello-world
httb://www.example.com/site2/article/index.php?q=goodbye-world -> httb://www.example.com/site2/article/goodbye-world
httb://www.example.com/site3/article/index.php?q=open-new-world -> httb://www.example.com/site3/article/open-new-world
There are multiple sites after example.com, I want to make the url look clean by using nginx configuration.
But my below configuration doesn't work. Someone help me?
server {
listen 80;
listen [::]:80;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
server_name www.example.com;
location ~ /article/ {
try_files $uri /site1/article/index.php?q=$1;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
You would like the client to present a URL like /xxx/article/yyy which is then internally rewritten to /xxx/article/index.php?q=yyy.
You need to capture the components of the source URI in order to use them later. You have a $1 in your question, but you are missing the expression to actually give it a value. With the minimum number of changes, this works:
location ~ ^(.*/article/)(.*)$ {
try_files $uri $1index.php?q=$2;
location ~ \.php$ { ... }
}
However, you do not need to use a nested location for PHP, as long as the PHP regex location appears above the other regex location, it will process all php files. For example:
location ~ \.php$ { ... }
location ~ ^(.*/article/)(.*)$ {
try_files $uri $1index.php?q=$2;
}

Variable username inside url address in NGINX

I still struggling with making mystaticusername variable user name. I mean when I have particular username /statisusername/ backend working properly on nginx. But how can I make my configuration for more users? Means use instead mystaticusername some redirection where each username will know login to backend.
How can I change my nginx configuration?
server {
server_name example.com www.example.com;
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/www;
charset utf-8;
index index.html index.php /index.php;
location = / {
rewrite ^ /index.php;
}
location / {
rewrite ^([^\.]*)$ /$1.php;
rewrite ^/([A-Za-z0-9_]+)$ /admin/index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location /mystaticusername {
try_files $uri/ /admin/index.php?q=$uri&$args;
}
location = /mystaticusername/options {
try_files $uri $uri/ /admin/index.php?hotelname=$1&do=options;
}
}
Not much context given, so I'm just guessin... but looks like you could condense your location directives to 3 statements.
This will default to index.php, but in the case of example.com/mystaticusername if would try that url and since it does not exist (guessing) it would serve up /admin/index.php?q=$uri&$args. This would also allow urls like exmaple.com/about to resolve. Just make certain to not allow users to create a username of 'about'
location / {
try_files $uri $uri/ /admin/index.php?q=$uri&$args;
}
Not certain what your goal is, but this would get you example.com/mystaticusername/options to try serve up /admin/index.php?hotelname=$1&do=options
location ~* ^/[a-zA-Z0-9]*/options$ {
try_files $uri /admin/index.php?hotelname=$1&do=options;
}
Leave php as-is
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}

Location route not matching

I have a location which simply for some reason isn't triggering. I've tried the routes in all sorts of different orders, and still it doesn't work. When a user comes along and requests /_hostmanager/ it should trigger, but instead it gets the index.php page from the route.
The server config file is:
server {
index index.php index.html;
root /var/www/html;
server_name _;
listen 80;
# Logging
error_log /var/log/httpd/elasticbeanstalk-error_log;
# Route standard requests
location / {
try_files $uri $uri/ /index.php;
}
# Proxy Hostmanager
location /_hostmanager/ {
proxy_pass http://127.0.0.1:8999/;
}
# Include PHP
location ~ \.php {
# CGI Configuration
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
# Zero-day exploit defense
try_files $uri $uri/ /index.php =404;
# Use socket for connection
fastcgi_pass unix:/tmp/php5-fpm.sock;
}
# Cache control
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
log_not_found off;
expires 360d;
}
# Disable hidden files
location ~ /\. {
deny all;
}
}
Can anyone spot what my (probably stupid!) error is?
Thanks in advance! :)
Nevermind, I worked it out! For some reason using the reload command on nginx wasn't working. stopped and started and voila!

Resources