nginx loading assets but not showing for wordpress app - css

I am using the following setup to run 2 applications on the same domain. If the first application returns 404 it tries the other one (wordpress on /blog).
location /blog {
root /var/www/blog;
index index.html index.htm index.php;
try_files $uri $uri/ /blog/index.php?$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;
}
}
location / {
# ...
proxy_intercept_errors on;
recursive_error_pages on;
error_page 404 =200 /blog/$uri;
}
Every page loads as expected and assets return 200 and the correct content.
But css and most images of those assets are not applied to the page, while a few work just fine.
The following is included:
include /etc/nginx/mime.types;
I understand that this is probably a bad configuration anyway but in my understanding it should still work.
Is there another configuration to achieve something similar?
EDITED:
The wordpress installation is located in /var/www/blog/blog/

I can't exactly understand the problem but I suggest you bring out that php block outside and remove blog and $args after index.php uri.
Try this one instead, let me know if this works out. :)
location /blog {
root /var/www/blog;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php;
}
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;
}
location / {
# ...
proxy_intercept_errors on;
recursive_error_pages on;
error_page 404 =200 /blog/$uri;
}

Related

Nginx setup for Wordpress does not link posts correctly

I currently have a WordPress site, which is currently installed on a subdirectory. However, there seems to be an issue.
The site appears as it supposed to, but the posts are either having a 404 error page and redirecting to the root of the site. Here is the screenshot of the setup I currently have on the server:
If anyone can please help me out here. I would appreciate it.
UPDATE (29/04/2018):
I have been working on this and it has come to a point where the blog page opens however, the posts are coming up with a 404 error message from the ROOT site.
For example:
When opening the subdirectory ( example.com/blog ), the site will appear. When clicking on a post, the 404 page will be displayed, but on the example.com site.
This makes me believe that there is an issue with the 2 WordPress installations however, I cannot be certain.
Here are my Nginx configurations for the root and the blog:
location /blog {
root /var/www/html/aus;
index index.php index.html;
access_log /var/log/nginx/blog.access.log;
error_log /var/log/nginx/blog.error.log;
try_files $uri $uri/ /blog/index.php$is_args$args;
if (!-e $request_filename) {
rewrite ^.*$ /blog/index.php last;
}
}
location / {
access_log /var/log/nginx/root.access.log;
error_log /var/log/nginx/root.error.log;
#try_files $uri $uri/ =404;
gzip off;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
reset_timedout_connection on;
include fastcgi.conf;
fastcgi_param SERVER_PORT 80;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
}
If you are getting 404 error page or some other error for posts, reset your permalinks setting.
Go to Settings → Permalinks and reset it to default settings and save it.
Your nginx config should be something like this:
upstream php-upstream {
server unix:/var/run/php5-fpm.sock fail_timeout=0;
}
...
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
reset_timedout_connection on;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_param SERVER_PORT 80;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-upstream;
fastcgi_index index.php;
}
...
That's an excerpt from a working nginx config for a wordpress website. Of course you should adapt it and modify it accordingly to suit your needs.

Nginx - Use different directory for serving files while stripping out path from URL

I am trying to use different directory from where to serve the files, but have been unsuccessful. /var/www/site.app/public is where the root, but I want to serve news from /var/www/news/api instead, while having URL http://site.app/news/123, from which I want to remove the news part, because otherwise it would map to /var/www/news/api/news/123.
Judging by the debug logs, it seems that it gets rewritten correctly when first testing all location blocks, but after it is done rewritting, it goes through all of them again, and ends up serving content with location block /.
Here is my configuration file I have.
server {
listen 80;
server_name site.app;
root /var/www/site.app/public;
rewrite_log on;
error_log /var/log/nginx/error.log debug;
index index.php index.html;
location / {
try_files $uri $uri/ index.php?$query_string;
}
location /news/ {
root /var/www/news/api;
rewrite ^/news/(.*) /api.php?_=$1;
index index.php index.html;
}
location ~ \.(hh|php)$ {
try_files $uri /index.php =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The root directive in your /news/ location currently does nothing. You are rewriting the URI to /api.php which then hits your \.(hh|php)$ location, which inherits the root from the parent server container.
You need a distinct location for /api.php, possibly /news/api.php. In which case you can construct a separate location block (possibly nested within /news/) where you can place fastcgi_pass code using the different root.
So this is not ideal solution, but it works. By default it would still allow access to api.php, but I am denying access to it by checking if the URL contains /news/
server {
listen 80;
server_name site.app;
root /var/www/site.app/public;
index index.php index.html;
location / {
try_files $uri $uri/ index.php?$query_string;
}
location = /api.php {
if ($request_uri !~ ^/news/) {
return 444;
}
root /var/www/site.app/news/;
try_files $uri $uri/ /api.php?$query_string =408;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /news/ {
rewrite ^/news/(.*) /api.php?$1;
}
location ~ \.(hh|php)$ {
try_files $uri /index.php =407;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

NGINX sef urls error: No input file specified

I've been struggling with a NGINX configuration. I've set up a development environment (local laptop) with a configuration supporting search engine friendly (SEF) urls, but the same configuration doesn't seem to work on my test server.
local configuration:
server {
server_name example;
root /home/arciitek/git/example/public;
client_max_body_size 500M;
location /collection/ {
try_files $uri $uri/ /collection/index.php$args;
index index.php;
}
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/arciitek/git/example/public/$fastcgi_script_name;
}
}
This works fine. Now on the test environment it looks like this:
{
server_name dev.example.com;
access_log /srv/www/dev.example.com/access.log;
error_log /srv/www/dev.example.com/error.log debug;
root /srv/www/dev.example.com/public;
location /collection/ {
try_files $uri $uri/ /collection/index.php$args;
index index.php;
}
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/dev.example.com/public$fastcgi_script_name;
}
}
On my development environment everything is fine. But on my test environment, when I call a url in my browser with prettyness added : collection/[brand]/[product]. I get the: No input file specified error. Mind you, if I call a url anding with collection/ everyting works as well..
Can anyone help me with this please? if more info is needed, please let me know..
kind regards,
Erik
After frustrating for a long time, I noticed that it was not the configuration that gave me trouble, but the link to sites-enabled...
There you go... *pads himself on the back...

Nginx vhost website + wordpress alias in subfolder

I want to create a vhost for a php website in this folder:
/var/www/mydomain.com/home
and an alias for this folder for a wordpress blog:
/var/www/mydomain.com/public_html
I try this vhost:
server {
listen 80;
root /var/www/mydomain.com/home;
index index.php index.html index.htm;
charset UTF-8;
server_name mydomain.com;
access_log /var/log/nginx/mydomain.com.access.log;
error_log /var/log/nginx/mydomain.com.error.log debug;
location /blog {
alias /var/www/mydomain.com/public_html;
try_files $uri $uri/ /index.php?$args =404;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
With this configuration, my website work fine, the homepage of my blog is OK, static files are OK, the page like http://mydomain.com/blog/wp-login.php are OK, all the admin panel is OK :
http://mydomain.com/blog/wp-admin/
(wp-admin folder exist)
But the page in front of like
http://mydomain.com/blog/terms-and-conditions/
http://mydomain.com/blog/contact/
or all the page with / like blog/.../... always go to my /var/www/mydomain.com/home/index.php
What did I forget to do?

Nginx location for PHP files that do not exist

I have config(minimal):
server {
listen test.local:80;
server_name test.local;
server_name_in_redirect off;
location / {
root /data/www/test/public;
try_files $uri $uri/ /index.php?route=$uri&$args;
index index.html index.php;
}
location ~ \.php$ {
root /data/www/test/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/test/public$fastcgi_script_name;
fastcgi_param ...
...
}
}
Thats work file for urls /help/ or /contacts/ etc… (all redirect to index.php with get variables).
But if url, for example, /help.php or contacts.php, and this files not exists, I have output:
File not found.
How update my Nginx config? I need URLs, for example:
/help.php => /index.php?route=/help.php
/contacts.php?foo=bar... => /index.php?route=/contacts.php&args=…
Here is simple config for 404 error page . Its custom 404 page.
error_page 404 /404.php;
location /404.php {
root /var/www/public_html;
allow all;
}
Last part
Important line is ========== try_files $uri =404;
location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/public_html$fastcgi_script_name;
include fastcgi_params;
}

Resources