The scenario is that I'd like to use Wordpress as a backend API provider for our Ember.js frontend app.
The Ember.js frontend needs to be served from the root, and the Wordpress instance ideally would be reachable by going to a subdirectory. So for example on localhost it would be http://localhost and http://localhost/wordpress
On the disk the two are deployed in /srv/http/ember and /srv/http/wordpress respectively.
I was trying to assemble the configuration going by the example on the Nginx site:
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
The config:
http {
upstream php {
server unix:/run/php-fpm/php-fpm.sock;
}
server {
listen 80;
server_name localhost;
root /srv/http/ember;
index index.html;
try_files $uri $uri/ /index.html?/$request_uri;
location /wordpress {
root /srv/http/wordpress;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
}
}
However this is obviously not the correct solution.
Upon trying to access the address http://localhost/wordpress/index.php I get the following in the logs:
2016/05/01 17:50:14 [error] 4332#4332: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /wordpress/index.php HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/php-fpm.sock:", host: "localhost"
The recipe isn't clear about where to put the root directive for the location of wordpress. I also tried with adding index index.php, which doesn't help either.
(Serving the Ember app works fine.)
From your question it seems that the location ~ \.php$ block is used by WordPress alone. However, it needs a root of /srv/http in order to find the script files for URIs beginning with /wordpress under the local path /srv/http/wordpress.
As there are two locations which both use the same WordPress root, it is possibly cleaner to make /srv/http the default (that is, inherited from the server block) and move root /srv/http/ember; into a separate location / block.
server {
listen 80;
server_name localhost;
root /srv/http;
location / {
root /srv/http/ember;
index index.html;
try_files $uri $uri/ /index.html?/$request_uri;
}
location /wordpress {
index index.php;
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php;
}
}
Notice that the default URI in location /wordpress is /wordpress/index.php and not /index.php as you originally had.
I have explicitly set SCRIPT_FILENAME as it may or may not appear in your fastcgi.conf file.
fastcgi_split_path_info has been removed as it is unnecessary in your specific case, and I think it would actually break WordPress the way you had it.
Related
In a subfolder on a domain I want to install a wordpress blog. I use nginx. The URL to access the blog should be like this: example.com/blog
site config looks as follows:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location /blog {
alias /var/www/example.comblog/html;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
location ~ /blog/.+\.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;
}
location ~ /\.ht {
deny all;
}
}
The wordpress files reside in the folder
/var/www/example.comblog/html. When accessing example.com/blog,
the browser shows a 404 error.
In /etc/php5/fpm/php.ini I adapted this: cgi.fix_pathinfo=0
nginx version: nginx/1.6.2
/var/log/nginx/error.log does not show anything of interest
UPDATE 1:
After setting error logging to debug, (among others) the following lines appear. Maybe this helps:
open index "/var/www/example.comblog/html/index.php"
internal redirect: "/blog/index.php?"
rewrite phase: 1
test location: "/blog"
test location: ~ "/blog/.+\.php$"
using configuration "/blog/.+\.php$"
http script var: "/blog/index.php"
trying to use file: "/blog/index.php" "/var/www/example.com/html/blog/index.php"
The internal redirect seems incorrect? And in the last line there should be /var/www/example.comblog/html/blog/index.php instead of /var/www/example.com/html/blog/index.php. I suspect this is the reason for the 404. Because the index.php does not exist at /var/www/example.com/html/blog/index.php.
Update 2:
Okay there seems to be a long standing issue with using alias together with try_files.
I am completely new to nginx .
I have project based on angular js which have a index.html, and on a certain event i perform a angular http request to x.php file and fetches the response from it.
Its running perfect on my local system and a apache based private hosting server.
I created a free tier ec2 instance and started a centos based linux instance on which i hosted the code and installed nginx .
Here is my nginx config
server {
listen 80;
server_name mydomain.co.in www.mydomain.co.in;
location / {
root /var/www/html/indm;
index index.php index.html index.htm;
try_files $uri/ $uri /index.php?$query_string =404;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?q=$uri&$args =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
add_header Access-Control-Allow-Origin *;
proxy_set_header Access-Control-Allow-Origin $http_origin;
}
error_page 500 502 503 504 /50x.html;
At first it was giving me 500 gateway error when the http request was performed on that php file. I check in the XHR request.Here is the error in nginx error.log
[error] 17193#0: *2 rewrite or internal redirection cycle while internally redirecting to "/index.php", client: 42.111.38.254
I searched a bit and changed appended a "=404" to try uri statement. But now it redirect to a 404 . I want to run that php file.
the server
Nginx server is running
php fpm is also running
Please help
The location ~ \.php$ block has no root. Move the root statement into the server block so that the same value is inherited by both location blocks.
For example:
server {
...
root /var/www/html/indm;
location / {
...
}
location ~ \.php$ {
...
}
...
}
I have been trying to host a simple Wordpress blog using NGINX as the web-server. The blog is hosted as a subdirectory under domain_name.com/blog.
The main blog opens up correctly. But when trying to open the wp-admin under domain_name.com/blog/wp-admin my browser shows ERR_TOO_MANY_REDIRECTS.
I am not sure if this is an issue with my NGINX configuration or wordpress configuration. Following is my NGINX server block:
server {
listen 80;
server_name <domain_name.com>;
root /var/www/html;
index index.php;
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Wordpress is installed under the /var/www/html/blog directory. And the values for "siteurl" and "home" wp_options in the database are both pointing to domain_name.com/blog.
What would be a good way to solve this problem?
Additional notes that might be helpful:
When I try to access static files under the wp-content directory, they open without any issues. No redirection errors there.
It is usual for WordPress to redirect an http session to https whenever accessing wp-admin. This may be controlled using the FORCE_SSL_LOGIN and FORCE_SSL_ADMIN settings in wp-config.php.
When a reverse proxy is terminating SSL, the fact that the originating connection is over https must be conveyed to WordPress to avoid a redirection loop.
Your reverse proxy should be setting headers such as X-Forwarded-Proto.
You need to change your nginx configuration so that the HTTPS flag is set correctly for WordPress.
For example:
map $http_x_forwarded_proto $https_flag {
default off;
https on;
}
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php;
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_param HTTPS $https_flag;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
With some (or very much) trial and error i was able to modify my copy and pasted nginx fastcgi php configuration from somewhere years ago to be able to run my php application in a subfolder.
But the last final step i am not able to solve is how to get nginx to pass the query string to php to be able to access the GET parameters. This is my configuration mostly perfect with only the configuration parameters missing:
server {
listen 80;
server_name project.dev;
location /app/ {
alias /path/to/my/application/;
index index.php;
try_files $uri $uri/ /app/index.php;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
location / {
# configuration for static website
}
}
I read that there are different options you have to pass to try_files to get request parameters:
try_files $uri $uri/ /app/index.php$is_args$query_string;
try_files $uri $uri/ /app/index.php$is_args$args;
try_files $uri $uri/ /app/index.php?$query_string;
Unfortunately changing it to any of these results in my php script no longer being found because nginx resets the request to it's document root:
2016/11/25 11:54:48 [error] 45809#0: *1169 open() "/usr/local/Cellar/nginx-full/1.10.2/htmlindex.php" failed (2: No such file or directory), client: 127.0.0.1, server: project.dev, request: "GET /app/myurl?test=works HTTP/2.0", host: "project.dev", referrer: "http://project.dev/app/myurl?test=works"
Providing an absolute path for fastcgi_param SCRIPT_FILENAME does not work too producting the same error. Even setting a root configuration on the server level does not work correctly, because the separating slash for the path and the index.php is omitted everytime. But (if possible) i would prefer without setting a root directory at the server level because this project is consisting of many different folders and applications on the filesystem sharing no common directory.
You have an application installed under /path/to/my/app2/public and would like to access it using the URI /app.
Assuming that we can use /app2/ as an internal URI (which does not collide with any other public URIs served by this server - but importantly will not be seen by your customers).
You have one PHP file.
location ^~ /app {
rewrite ^/app(.*)$ /app2/public$1 last;
}
location ^~ /app2/ {
internal;
root /path/to/my;
index index.php;
try_files $uri $uri/ /app2/public/index.php$is_args$args;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /path/to/my/app2/public/index.php;
}
}
The first location block simply alters the internal URI to match the document root (so we can use root instead of alias). The second location block serves the static content. The third location block invokes index.php.
How index.php gets the query string is program dependent. It will use one of the parameters defined in fastcgi_params. Usually either REQUEST_URI or QUERY_STRING. Either way, both variables should be preserved with the above configuration.
The ^~ modifier ensures that these location blocks take precedence over other regular expression location blocks (should any exist). See this document for details.
I am trying to setup a Nginx server configuration to serve a CakePHP installation from and to a subfolder.
URL: https://sub.domain.com/cakefolder
Folder on system: /var/www/domain/sub/cakefolder
So i am using a sub folder for both the URL as well as on the system. Now it took me a while to figure the following config out with which requests are properly handled by CakePHP. This means it's correctly bootstraping and handling controllers.
What doesn't work however, is serving static files from the webroot directory (e.g. *.css files) as those are all interpreted as CakePHP controllers leading to a CssController could not be found. error.
My site.conf:
server {
listen *:80;
listen *:443 ssl;
server_name sub.domain.com;
ssl_certificate ./ssl/domain.crt;
ssl_certificate_key ./ssl/domain.key;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
if ($ssl_protocol = "") {
rewrite ^ https://$server_name$request_uri? permanent;
}
root /var/www/domain/sub/cakefolder/;
autoindex off;
index index.php;
location /cakefolder {
root /var/www/domain/sub/cakefolder/app/webroot/;
try_files $uri $uri/ /cakefolder/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
How do I stop Nginx from routing existing static files through the FastCGI PHP interpreter?
Based on https://stackoverflow.com/a/22550332/671047 I already tried replacing my location /cakefolder { ... } with
location ~ /cakefolder/(.*) {
try_files /cakefolder/$1 /cakefolder/$1/ /cakefolder/index.php?$args;
}
but this leads to a redirection loop causing a HTTP 500 error.
Solution (thanks Pete!):
I found the following additional directive to be working for this specific setup. This might not be the most elegant solution but who cares, glad it's working for now.
location ~* /cakefolder/(.*)\.(css|js|ico|gif|png|jpg|jpeg)$ {
root /var/www/domain/sub/cakefolder/app/webroot/;
try_files /$1.$2 =404;
}
you could catch it early:
location ~* \.(css|js|ico)$ {
try_files $uri =404;
}
i have a similar setup and that worked for me when i experienced the same thing (just not cake.) I won't lie, i never understood why the try_files w/redirect always failed on existing static files, where as throwing a try_files like ^above finds the file np. ideas on that? (perhaps today is a source-reading day)