symfony2, wordpress and nginx configuration - wordpress

I have symfony2 on the main page, now i tried to install WordPress on the sub directory called blog.
I'm using nginx as web server with fastcgi. I tried to configure the it but i got the error 500.
Does anyone know how to set it up properly on nginx?
Thanks

rewrite ^(.*) /app_dev.php last;
Should probably be something like
rewrite ^(.*) /web/app_dev.php last;
You also probably rather want to use try_files like so, else your images, css, js assets won't be found. Something like
location / {
# try to serve file directly, fallback to rewrite
try_files $uri /web/$uri #rewrite;
expires 30d; ## Assume all files are cachable
location ~ \.php {
expires off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
add_header X-Frame-Options "ALLOW-FROM https://www.youtube.com/" ;
}
}
location #rewrite {
rewrite ^/(.*)$ /web/app.php/$1 last;
}
location /blog {
//Wordpress routing here
}
Anyway that is how I'm doing it with Magento instead of Wordpress.

Related

Nginx rewrite to prevent PHP file direct access

I'm trying to setup some rewrites so that
(1) .com/images/* will load naturally
(2) .com/* will be rewritten to .com/loader.php?control=*
However the code below works perfectly except it will execute .com/config.php instead of rewriting it to .com/loader.php?control=config.php
How can I prevent my rewrite being overridden? I only want .php files to be executed if it's loader.php or in the images folder. (Been trying for hours)
server {
listen 80;
root /mnt/web/test_com/public_html;
server_name test.com;
index index.html index.php index.htm;
location ~ .php(.*)$ {
fastcgi_pass unix:/tmp/php-70-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
fastcgi_param PATH_INFO $2;
include fcgi.conf;
}
location / {
rewrite ^/(.*)$ /loader.php?control=$1 last;
}
location /images/ {
}
}
Thanks!
You should look into the try_files directive
Like the return and rewrite directives, the try_files directive is placed in a server or location block. As parameters, it takes a list of one or more files and directories and a final URI
Read the whole blog-post written on the nginx blog here:
https://www.nginx.com/blog/creating-nginx-rewrite-rules/

Nginx load subpath as wordpress root

I'm trying to set up a Wordpress in a system that has another php application installed, using nginx as web server.
I've simplified my config file to the maximun. The following confi is serving one post of my blog:
server {
listen 80;
server_name blog.ct.com;
root /home/ff/www/blog/;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$uri&$args =405;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_buffer_size 128k;
fastcgi_buffers 64 32k;
fastcgi_busy_buffers_size 128k;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param APPLICATION_ENV development;
fastcgi_param HTTP_X_FORWARDED_PROTO https;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
But, due my system's requirements I need to serve the blog from within a sub path (In my final system http://blog.ct.com/ should be serving my custom php app and http://blog.ct.com/vendor should be serving the wordpress blog).
The local root directory from wordpress must be /home/ff/www/blog/ (this cannot be changed, while my custom app's directory is /home/ff/www/myapp/). So I think I need to reserve location / for my custom app, I have to create a location /vendor
If I add /vendor and I return 403 in / (just to debug easier), the browser says 405 (notice the =405 in /vendor, also to debug easier):
location /vendor {
try_files $uri $uri/ /index.php?$uri&$args =405;
}
location / {
return 403;
}
So I think nginx is going into location /vendor but is not finding my php script in /home/ff/www/blog/index.php so its returning the fallback 405.
Any idea why this could happen?
How can I achieve to load http://blog.ct.com/vendor as the root from wordpress but keeping http://blog.ct.com/ using another php script?
I've found out the following hints that gave me the clue to fix the problem (in case someone has the same problem than me, this may help)
Using location /path is not the same as using location ~(/path) (regex have different priority, so maybe they are not being checked in the order you think)
Adding error_log /your/path/log/error.log debug; to any location block may help you to see how is nginx serving every request (e.g. to location fastcgi, location \vendor, or the server{ block).
alias /var/www/path/vendor works different than root /var/www/path/vendor (check Nginx -- static file serving confusion with root & alias);
In case of the root directive, full path is appended to the root including the location part, whereas in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias.
using rewrite with alias can help you parse the php file you want independent of the path
if (!-f $request_filename) {
rewrite ^ $document_root/index-wp.php last;
}
Take care of the SCRIPT_FILENAME you are using (check it with error_log, see above), maybe you need fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; but you are loading fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; so depending on your previous config you may be attaching the document root twice.
Two different configurations for fastcgi can be used if you change your index.php file names. E.g. location ~ wp\.php$ { will work with wp.php while location ~ \.php$ { will work with all other php files like index.php.

nginx + php-fpm Rewrite rule not working

I have this conf,
rewrite "^download/([0-9a-f]{32})/(.+)$" /download.php?h=$1&f=$2 last;
location / {
index index.html;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.apache.sock;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME $root_path$request_filename;
include fastcgi_params;
}
But when I try open url, http://example.com/download/d3ef6bbeaff9b429680bca646e8ee1cf/video.mp4
It's return 404 Not Found
I tried put rule in any location, but it's not helped.
Direct link to file http://example.com/download.php is working, what need to do for work rewrite ?
I saw many threads about it, but solutions not helped me, whats wrong?
On server nginx + php-fpm
Nginx's rewrite always match full URI that starts with slash. So you need to fix your rewrite:
rewrite "^/download/([0-9a-f]{32})/(.+)$" /download.php?h=$1&f=$2 last;

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.

Moodle 2.3 with Nginx vs slash argument rewrite

I'm trying to setup Moodle 2.3 (not 2.5) ver with nginx latest build. There was some advice on this site before. One of them: Moodle 2.0 with Nginx backend.
Apparently as anybody knows, Moodle is using path_info rules to post URL's like this: http://example.com/moodle/pluginfile.php/26/mod_scorm/content/1/index.html. To escape all this nightmare, Moodle is offering to disable "Slash arguments" in UI. Which is great. But not for SCORM player which is forcing "Slash argument" despite the previous option. So with disabled "Slash arguments" everything is working and normal. But my only goal is to use SCORM player.
I tried to use the rewrite rule from the link above:
rewrite ^(.*\.php)(/)(.*)$ $1?file=/$3 last;
which is not working in 2.3-2.5 ver. I assume it worked in 1.9.
Now Moodle is using different path:
http://example.com/moodle/pluginfile.php/26/mod_scorm/content/1/index.html
Some of nginx rules:
location ^~ /moodle {
location ~* ^.+\.(?:css|js|htc|xml|jpe?g|gif|png|ico|bmp|svg|swf|pdf|docx?|xlsx?|tiff?|txt|rtf|cgi|bat|pl|dll|aspx?|class|otf|ttf|woff|eot|less)$ {
add_header Access-Control-Allow-Origin *;
access_log off;
expires 30d;
tcp_nodelay off;
try_files $uri =404;
}
location ~* ^/moodle/.*\.php$ {
include includes/fastcgi_params.conf;
try_files $uri #dynamic;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_read_timeout 1200;
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9090;
}
rewrite (^.*\.php)(/.*) $1 last;
}
Please advise how to solve this.
(Answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I solved this by putting rewrite directive in {server} not in {location} section. In my scenario moodle is installed under subfolder: example.com/moodle.
server {
server_name example.com www.example.com;
rewrite ^/moodle/(.*\.php)(/)(.*)$ /moodle/$1?file=/$3 last;
location ^~ /moodle {
try_files $uri $uri/ /index.php?q=$request_uri;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9090;
include includes/fastcgi_params.conf;
}
}
}

Resources