Setting up Wordpress and Rails in Nginx - php

I have nginx set up for Rails application that has been working beautifully for me. Now I want to move my Wordpress blog from blog.website.com to website.com/blog, so web crawlers treat it as part of the site.
I created a symbolic link in my Rails app's public/ directory and added the following to my nginx configuration:
# Rails server
server {
root /project/path/current/public;
server_name project.com;
passenger_enabled on;
rails_env production;
client_max_body_size 20m;
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
location /blog {
index index.html index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
if (!-e $request_filename) {
rewrite ^.+?(/blog/.*.php)$ $1 last;
rewrite ^ /blog/index.php last;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
This works great.
But what I'd like to do is skip the symbolic link part. I'd like nginx to route directly to the wordpress directory. I tried changing the /blog block to:
location ^~ /blog {
root /home/ubuntu/apps/blog;
index index.html index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
if (!-e $request_filename) {
rewrite ^.+?(/blog/.*.php)$ $1 last;
rewrite ^ /blog/index.php last;
}
}
This works, as in I'm being correctly redirected to the wordpress folder but my server doesn't know what to do with php files and sends them to my browser as files to download.
I tried nesting the \.php$ location inside the /blog location but it results in 404 error.
What's wrong with the following piece and how to fix it?
location ^~ /blog {
root /home/ubuntu/apps/blog;
index index.html index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
if (!-e $request_filename) {
rewrite ^.+?(/blog/.*.php)$ $1 last;
rewrite ^ /blog/index.php last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}

I haven't fixed the snippet from the question but I managed to solve the problem with nginx configuration without using a symbolic link by using two server blocks.
server {
location / {
proxy_pass http://localhost:82/;
}
location /blog {
root /var/proj/blog/current;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
# Rails server
server {
listen 82;
root /var/proj/site/current/public;
server_name site.com;
passenger_enabled on;
rails_env staging;
client_max_body_size 20m;
# other configuration
}

Related

Rewrite requests to different enpoint with nginx

I have site on joomla and subfolder folder with standalone php files.
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
fastcgi_index index.php;
fastcgi_pass backend;
fastcgi_read_timeout 90;
}
Now I want to do something like this:
location ~ /forder/(.*)\.php {
try_files $uri /folder/app.php?controller=$1;
}
When I go to https://site/folder/script.php nginx send source code of that file.
Where I go wrong?
Solution for me is to put
if (!-e $request_filename){
rewrite /folder/(.*)\.php /folder/app.php?controller=$1 last;
}
in server section

Redirecting to controller/action

I want to be able to convert the following link:
http://domain.com/foo/bar
into http://domain.com/index.php?controller=foo&action=bar
using php5-fpm. I also want to be able to access static files inside www/ folder. How do I do that? This is what I have so far:
server {
charset utf-8;
client_max_body_size 8M;
listen 80; ## listen for ipv4
server_name domain.com;
root /var/www/domain.com/www;
index index.php;
access_log /var/log/nginx/domain.com.access.log;
error_log /var/log/nginx/domain.com.error.log;
location / {
rewrite ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /index.php?controller=$1&action=$2;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(ht|svn|git) {
deny all;
}
}
But it gives me blank page and no get parameters. How should I do this?
The try_files directive is useful when serving static files, if they exist, and rewriting the URI if they do not. See this document for details.
There are a number of ways to achieve this, for example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /index.php?controller=$1&action=$2 last;
return 404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}

Query strings in NGINX

I have problem with my configuration server on nginx.
My configuration:
server {
listen 80;
server_name shop.md;
index index.php index.html index.htm;
access_log /var/log/nginx/test.dev.access.log;
error_log /var/log/nginx/test.dev.error.log;
location / {
root /home/vagrant/Workspace/shop/web;
try_files $uri $uri/ app_dev.php /app_dev.php$is_args$args;
}
location ~ .php$ {
root /home/vagrant/Workspace/shop/web;
index index.html index.htm index.php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APPLICATION_ENV development;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
}
sendfile off;
}
This configuration allow urls like this :
http://shop.md:8000/1/femei-pantofi
http://shop.md:8000/1/femei-pantofi?min_price=1&max_price=1000
For this URL:
http://shop.md:8000
I get the error 403 Forbidden
I use the #rewriteapp directive of nginx for my Symfony2 projects. The resulting configuration looks somewhat like this:
# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(app|app_dev|adminer)\.php(/|$) {
fastcgi_pass phpfcgi-siyabonga;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
This works fairly well for me and is taken from the official nginx wiki page about Symfony2 with some additional changes. Also you should check the official Symfony2 docs. They have an example of a correct nginx configuration.

Run HHVM and PHP simultaneously with Nginx

I've been running a magento site using HHVM and Nginx. So far I hadn't experienced any problems and all I got was a very welcomed noticeable performance boost.
However, I've just added a plugin which uses PHP functions which are unsupported by HHVM. The good news is that this plugin is only needed for one page. Bad news is I'm not configuring Nginx right to serve this page only with PHP.
The usual fallback trick used by some people using error directives does not work in this case because the page doesn't throw an error. It just doesn't work if HHVM is enabled.
Instead, I've tried to write many variations of location blocks for that particular page. None worked, and these are the two that I had thought would do the trick.
Is there a way to run PHP only for a specific page?
FAILED SOLUTION 1
location ~ /page/.+\.php${
if (!-e $request_filename) { rewrite / /index.php last; }
fastcgi_pass unix:/var/run/php5-fpm.sock; ##Unix socket
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.(hh|php)$ {
if (!-e $request_filename) { rewrite / /index.php last; }
fastcgi_keep_conn on;
fastcgi_pass unix:/var/run/hhvm/sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS $fastcgi_https;
include fastcgi_params;
}
FAILED SOLUTION 2 (with nested location)
location ~ /page/ {
location ~ .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
fastcgi_pass unix:/var/run/php5-fpm.sock; ##Unix socket
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ \.(hh|php)$ {
if (!-e $request_filename) { rewrite / /index.php last; }
fastcgi_keep_conn on;
fastcgi_pass unix:/var/run/hhvm/sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS $fastcgi_https;
include fastcgi_params;
}
Try using variable condition approach, it works for me, locations starting with /serve-with-php/ are being served with unix:/var/run/php5-fpm.sock, while all the others are being served with 127.0.0.1:9000
server {
root /home/vearutop/test-hhvm;
index index.php index.html index.htm;
error_log /var/log/nginx-error.log error;
charset utf-8;
server_name test-hhvm;
location / {
set $fcgi_worker 127.0.0.1:9000; # hhvm handle
try_files $uri $uri/ /index.php?$query_string;
}
location /serve-with-php/ {
set $fcgi_worker unix:/var/run/php5-fpm.sock; # php-fpm handle
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass $fcgi_worker;
fastcgi_index index.php;
include fastcgi_params;
}
}

Clean URLs Nginx Rewrites

After searching the interweb for hours and none of the "guides" working I turn to you guys and girls for help.
I am attempting to clean all my urls for all files in any directory so www.foo.com/foo.php becomes www.foo.com/foo.
Also I want to tidy up requests such as www.foo.com/foo.php?foo=foo to www.foo.com/foo/foo
Here is my current failed attempt:
server {
# Change these settings to match your machine
listen 80 default_server;
server_name example.com www.example.com;
# Everything below here doesn't need to be changed
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
root /var/www/example.com;
try_files $uri $uri/ #extensionless-php;
index index.html index.htm index.php;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
# The next two lines should go in your fastcgi_params
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
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 ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
}
It works but I can still access the old .php url aswell which as far as im aware is bad for SEO? as its duplicate content?
Thanks in Advance
Danny

Categories