Nginx rewrite to make a "personal URL" - nginx

I'd like to be able to make "personal URL" for our users (Facebook like), which is of course a dynamic strings. it needs to be in the root of the site, and that is why I'm having a big headache with it.
The requirements that I have are:
1. I need
www.example.com/John.Doe (it can be a-zA-Z0-9_-.)
and rewrite it to:
www.example.com/profile?id=John.Doe
2. I also need the site scripts to be extension less like (which I was able to do, with the great people here, using "$uri.php$is_args$query_string;"):
so
www.example.com/login
will go to:
www.example.com/login.php
I tried a lot of things, but I just can't get the right formula to make it work.
This is my configuration, right now:
location / {
try_files $uri $uri/ $uri.php$is_args$query_string;
}
location ~ \.php$ {
if ($request_uri ~ ^/([^?]*)\.php(\?.*)?$) {
return 301 /$1$2;
}
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

You have an overlapping namespace for your extension-less scripts and your personal URLs, so you need to test for the presence of the $uri.php file before rewriting it to profile.php.
So rather than rewrite the URI in the first try_files directive (as you have it now), it may be better to use a named location block to process the PHP file without having to rewrite it first.
Like this:
location / {
try_files $uri $uri/ #php;
}
location #php {
try_files $uri.php #rewrite;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9001;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #rewrite {
rewrite ^/([a-zA-Z0-9_.-]+)$ /profile.php?id=$1 last;
}
location ~ \.php$ {
if ($request_uri ...) { ... }
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9001;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
The first block serves static files. The second block processes extension-less PHP files (if and only if they exists). The third block performs a rewrite to profile.php (which does not need to be extension-less, as it is not exposed to the client). The fourth block processes normal .php URIs and includes your $request_uri fix.
Note that fastcgi_index is redundant in both this and the original configuration.
For more details, here are some links to the nginx documentation for the location, try_files and rewrite directives.

Related

Different url rewriting according to locations

I really don't find any documentation I find clear about URL rewriting (I can't understand it, as I unexpectedly find the documentation really hard to read for a non-native).
I'm looking for a way to rewrite all routes that matches /*\.(js|png|jpg|css|ttf|xml)$/ toward path/media/ and try existance of file then return it if exists, else 404 not found
then if it begins with /ajax/ redirect all of it toward path/ajax/index.php
else redirect all of it toward path/www/index.php
I don't quite understand how I should do it, for now I created 3 locations /media/, /ajax/ and /www/ but I don't know if it is the right way to use rewrite and not return, or are the locations the correct way to do it.
I don't really understand what I've written in my sites-enabled/file regarding fastcgi. is this a interpretor path ?
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
If I get it right, it means "if it ends with .php, and it exists in hierarchy, then execute it".
And I don't know if I should put that kind of stuff for each location that has to deal with php (/www/ and /ajax/), especially since I'm going to do some routing for both. Moreover, I don't know if it should be done that way.
The simplest PHP configurations use a common root directive which is inherited by the location blocks, and in your case would be:
root path;
This means that /www/index.php and /ajax/index.php are both processed by the location ~ \.php$ block.
The default action can be defined by the try_files directive within a location / block:
location / {
try_files $uri $uri/ /www/index.php;
}
If you need a different default action for URIs which begin with /ajax, add a more specific location:
location /ajax {
try_files $uri $uri/ /ajax/index.php;
}
If you do not want your media URIs to begin with /media you can override the root for one specific location:
location ~* \.(js|png|jpg|css|ttf|xml)$ {
root path/media;
}
The fastcgi_split_path_info and fastcgi_index directives are unnecessary in your specific case. The include fastcgi_params; statement should be placed before any fastcgi_param directive to avoid the latter being inadvertently overridden:
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
See the nginx documentation for details.

Laravel and WordPress integration routing using nginx

I am developing web app in Laravel 5.2. I have existing WordPress site. So, I want to integrate Laravel with WordPress. WordPress app has static pages. I have two separate directories for Laravel and WordPress in my root directory.
laraApp
wpApp
I want to make wpApp as default app. So when user clicks login button, user will be redirected to laraApp. I want wpApp at www.example.com and laraApp in www.example.com/laraApp. I have nginx web server running. So what should be my nginx config file?
Current nginx config file is :
server {
listen 80;
root /var/www/root/wpApp;
index index.php index.html index.htm;
server_name www.example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# rewrite rules for laravel routes
location /laraApp {
rewrite ^/laraApp/(.*)$ /laraApp/public/index.php?$1 last;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Here my Laravel app is accessible using url www.example.com/laraApp/public/
I want to access it using www.example.com/laraApp.
Thanks.
The configuration would be simpler if the base URI for each of the applications did not overlap. But given the constraints of your question, you must use two distinct document roots for the PHP section of each of the applications in your configuration.
As you have placed one of your applications in /, the other application is kept separate by the use of nested location blocks. Notice the use of the ^~ modifier to prevent the first PHP block from processing Laravel requests.
index index.php index.html index.htm;
root /var/www/root/wpApp;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ^~ /laraApp {
rewrite ^/laraApp(.*)$ /laraApp/public$1 last;
}
location ^~ /laraApp/public {
root /var/www/root;
try_files $uri $uri/ /laraApp/public/index.php?$query_string;
location ~ \.php$ {
try_files $uri /laraApp/public/index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
I am away from my test system at the moment so the above has not been syntax checked or tested.

How to install one site inside another with Nginx

I have a wordpress multisite on centos 6.5 at /var/www/html/site1
accessible at site1.com and everything there is fine.
I would like to install a single site called site2 at
/var/www/html/site2 but
accessible at site1.com/site2
The .conf for site1 has a server block like this:
location / {
try_files $uri $uri/ /index.php?$args;
}
#editing here to...
location /site2 {
root /var/www/html/site2/;
index index.php index.html index.htm;
try_files /index.html /index.php $uri $uri/;
}
location ~ /site2/(.*\.php)$ {
root /var/www/html/site2/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$1;
include /etc/nginx/fastcgi_params;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
#end editing.
When I use try files try_files /index.html /index.php $uri $uri/; it works.
When I use try_files try_files /index.php $uri $uri/; the browser downloads the file instead of displaying it.
The location block should do the trick as long as you define a new root for the location:
location /site2
{
root /var/www/html/site2;
#put your rewrite rules here
}
You have to make sure that your fastcgi (I assume you'r using php-fpm) configuration uses the new docroot as well.
I don't know wordpress very well, but I reckon you also have to configure the second site to be in a subdirectory called /site2, otherwise links might not work properly.

Short-circuit logic in Nginx conf (would like to override a location)

I have mediawiki installed. All is right in the world except for when I try to alias a external directory (webalizer web stats). I see that Nginx passes off the request to /usage/* to PHP/Mediawiki. I don't want that. I literally want everything under /usage/ to point to my alias and nothing else. Completely separate from Mediawiki code and functionality.
# in no way related to Mediawiki. I just want to serve this as static HTML.
location /usage {
alias /var/www/webalizer/wiki.longnow.org/;
}
# This answers to anything, which may be my problem
location / {
try_files $uri $uri/ #rewrite;
index index.php;
}
# A special rewrite to play nicely with Mediawiki
location #rewrite {
rewrite ^/(.*)$ /index.php?title=$1&$args;
}
# PHP, nom nom nom
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php-fastcgi.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
I was hoping that listing the /usage location directive ahead of the rest would short-circuit the system, but I have been spoiled by Django ;)
To stop Nginx from processing further location directives, it should be prefixed by ^~.
I think you will still want a try_files falling back to a 404 response inside the location.
location ^~ /usage {
alias /var/www/webalizer/wiki.longnow.org/;
try_files $uri $uri/ =404;
}
See http://wiki.nginx.org/HttpCoreModule#location for reference.

Configuration for lithium on nginx

I would like to deploy lithium on nginx server, however there are configurations provided only for Apache and IIS.
I've successfully written several nginx server configurations for various applications in past, but I'm struggling with this one.
Already asked this question on nginx and lithium forums, no luck.
This is best of what I've made so far.
root /var/servers/my_app/app/webroot;
location / {
index index.php;
try_files $uri $uri/ index.php;
}
location ~ \.php {
fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/servers/my_app/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
Problem is on / (root page) every link gets index.php prepended, e.g. instead of
www.example.com/something
I get
www.example.com/index.php/something
Not sure if this even is nginx configuration related or rather something, that lithium does when it cannot detect Apache/IIS environment. Either way I cannot solve it.
Another thing, that when I access "www.example.com/test" (via direct URL input), the page renders correctly, however "www.example.com/test/" (with trailing slash) and "www.example.com/test/anything_here" is broken - all links gets appended to current URL e.g. pressing the same link creates this:
www.example.com/test/
www.example.com/test/test
www.example.com/test/test/test
EDIT: Updated configuration
(Sorry for much delayed edit, but I'm still stuck and recently restarted solving this)
root /var/server/my_app/app/webroot/;
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
location ~ \.php {
fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/servers/my_app/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
location ~/\.ht {
deny all;
}
}
As I mentioned in comments this now causes all links to have index.php included, it looks like:
www.example.com/index.php/something
www.example.com/index.php/stylesheet.css
I think your problem is that the try_files shouldn't be inside a location block.
Try the configuration shown here: http://li3.me/docs/manual/configuration/servers/nginx.wiki
I helped define it and have been using it locally and in production. It shouldn't cause any of the issues you're reporting.
Copying it below:
server {
listen IP_ADDRESS_HERE:80;
server_name DOMAIN.COM;
root /var/www/DOMAIN.COM/webroot/;
access_log /var/log/DOMAIN.com/access.log;
error_log /var/log/DOMAIN.com/error.log warn;
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
As I suspected the problem was that Lithium relies on some environment variables, in this case - link generation, it uses PHP_SELF, which happened to be incorrect.
Solution:
fastcgi_param PATH_INFO $fastcgi_path_info;
Instead of previously incorrect:
fastcgi_param PATH_INFO $fastcgi_script_name;
So final configuration:
root /var/server/my_app/app/webroot/;
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
location ~ \.php {
fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/servers/my_app$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~/\.ht {
deny all;
}
Thanks to rmarscher and mehlah # lithum forums.

Resources