Some Issues with Configuration

Hi

I’ve been having a few problems with configuration of NGINX. No
problems with running Apache or Lighttpd on my own Linux box but I’ve
been scratching my head over NGINX.

When I’ve compiled from source or used the vanilla Ubuntu package I
find that I can download the front page from my box which is
http://sleepypenguin.homelinux.org. This is an HTML page. I can’t
download other pages which are serveral instances of Wordpress. Such
as http://sleepypenguin.homelinux.org/blog. What I get instead is
that my web browser (I have tried several web browsers in different
locations) asks me to download a BIN file instead of a web page.

I’m sure that someone has had this problem when configuring NGINX. Can
anyone point me in the right direction ? I thought it might be
something to do with the location statment. Such as …

/etc/nginx/common/locations.conf

Blog

location = /blog {
allow all;
access_log off;
log_not_found off;
}

But… No… That’s not it either.


Richard
Sheffield UK

https://twitter.com/SleepyPenguin1

On 5 March 2014 11:13, Richard I. [email protected]
wrote:

as http://sleepypenguin.homelinux.org/blog. What I get instead is
location = /blog {
allow all;
access_log off;
log_not_found off;
}

But… No… That’s not it either.

Nginx doesn’t execute PHP. It passes each request destined for your
blog (i.e. the locations you decide are “your blog”) to another
process that runs/is-running the PHP. Take a look here, and it might
help: http://wiki.nginx.org/WordPress

If you’re still stuck, please have a google before asking more
questions here. There are many, many, many articles out there,
explaining how to get Nginx+PHP/WordPress working, and the config you
posted above strongly suggests you’ve not read any of them yet! The
Internet is your friend … :wink:

Jonathan

On Wednesday 05 Mar 2014 21:03:22 Jonathan M. wrote:

Nginx doesn’t execute PHP. It passes each request destined for your
blog (i.e. the locations you decide are “your blog”) to another
process that runs/is-running the PHP. Take a look here, and it might
help:

http://wiki.nginx.org/WordPress<<

I’ll have a read through this again. The part that might work is…

location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}

    location ~ \.php$ {
            fastcgi_split_path_info ^(/blog)(/.*)$;
    }

But… I have not just /blog but others. Such as /journalism and
others. Do I just put in another location for that ? Such as …

location /journalism {
try_files $uri $uri/ /journalism/index.php?$args;
}

    location ~ \.php$ {
            fastcgi_split_path_info ^(/journalism)(/.*)$;
    }

If you’re still stuck, please have a google before asking more
questions here. There are many, many, many articles out there,
explaining how to get Nginx+PHP/WordPress working, and the config
you posted above strongly suggests you’ve not read any of them yet!
The Internet is your friend … :wink:

I spent a month doing that. Been going to ApacheCon since 2001. Done
most international GNU/Linux and BSD conferences. Seen a few things.
I’m a bit lost on NGINX configuration. Something new to learn :slight_smile:


Richard
www.sheflug.org.uk

On 5 March 2014 21:18, Richard I. [email protected]
wrote:

location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}

    location ~ \.php$ {
            fastcgi_split_path_info ^(/blog)(/.*)$;
    }

Your indenting is/might-be misleading. Are you intending to
encapsulate the PHP regex inside the /blog prefix, as your indenting
suggests? 'Cos your braces aren’t doing that.

But… I have not just /blog but others. Such as /journalism and
others. Do I just put in another location for that ? Such as …

location /journalism {
try_files $uri $uri/ /journalism/index.php?$args;
}

    location ~ \.php$ {
            fastcgi_split_path_info ^(/journalism)(/.*)$;
    }

In general, each request is handled in one location and one location
only in nginx. There are some directives which inherit down config
levels (http{}, server{}, location{}, nested location {}) but they’re
not the ones you’ll (probably) be caring about here.

If you can, pop your blog on its own FQDN so you can seperate out
traffic into different server{} stanzas. That works well as a base
level of distinction between traffic which should hit PHP and that
which shouldn’t.

If you’re still stuck, please have a google before asking more
questions here. There are many, many, many articles out there,
explaining how to get Nginx+PHP/WordPress working, and the config
you posted above strongly suggests you’ve not read any of them yet!
The Internet is your friend … :wink:

I spent a month doing that. Been going to ApacheCon since 2001. Done
most international GNU/Linux and BSD conferences. Seen a few things.
I’m a bit lost on NGINX configuration. Something new to learn :slight_smile:

Apologies - I assumed from the config you posted you’d not done any
reading :slight_smile:

Nothing you’ve posted yet specifies the method you’re using to talk to
your PHP-executing process. Perhaps you should post a more complete
config and let us know what you’ve tried already …

J

Hi!

On Wed, 2014-03-05 at 21:18 +0000, Richard I. wrote:

location /blog {
location /journalism {

you posted above strongly suggests you’ve not read any of them yet!
The Internet is your friend … :wink:

I spent a month doing that. Been going to ApacheCon since 2001. Done
most international GNU/Linux and BSD conferences. Seen a few things.
I’m a bit lost on NGINX configuration. Something new to learn :slight_smile:

Nginx is just acting as a switch here, so in the location block for .php
files, you need to hand over to php for further processing. This is
usually done via fastcgi to php-fpm - especially for the performance
gains after using APC, etc…

You can do this in one of two ways: I use the more convoluted one…

in nginx.conf, define a php backend:

http {

upstream backend {
server unix:/var/run/php5-fpm.sock;
}
}

( ensure that the php-fpm process is listening on that socket - you can
also use ports )

in the site config pass them over:

location ~ .php$ {
fastcgi_split_path_info ^(/journalism)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass backend;
}

That should approximately do it - depending on the contents
of /etc/nginx/fastcgi-params which I modified years ago!

hth,

Steve

Steve H. BSc(Hons) MIITP
http://www.greengecko.co.nz
Linkedin: http://www.linkedin.com/in/steveholdoway
Skype: sholdowa

On Sat, Mar 08, 2014 at 09:02:37PM +0000, Richard I. wrote:

Hi there,

NGINX is serving up static HTML pages but I now find that WordPress
pages are being served as plain text.

nginx uses a single config file.

There is an “include” directive where other files are (effectively)
copied in to the configuration when nginx reads it.

Any file not mentioned in an “include” directive is irrelevant to nginx.

http://nginx.org/en/docs/http/request_processing.html

When a request comes in, nginx uses the “listen” and “server_name”
directives in every “server” block to choose the one “server” block that
will handle this request.

If you only have a single “server” block, that’s the one that will be
used. If you have more than one, make sure that you can know which one
will be used for the request you are making.

After any server-level rewrite-module directives, nginx chooses a
single “location” block in which to process the request. In that block,
there will either be a directive that causes the request to be handled
elsewhere – e.g. proxy_pass or fastcgi_pass – or there is a default
“serve it from the filesystem”.

(There are also subrequests and internal rewrites within nginx; there
may be more than one nginx request associated with a single incoming
http request.)

So, in each case where the output is not what you want, you should be
able to say something like

I request http://example.com/blog
I want a 301 redirection to http://example.com/blog/
I get a 301 redirection to http://www.example.com/blog/

or

I request http://example.com/blog/
I want the php-processed content of the file /var/www/blog/index.php
I get the unprocessed content of the file /var/www/blog/index.php

and with that information, you should be able to see which one location
was used to process the request; and you should be able to see why the
response was what it was.

In your specific case of unprocessed content, the most likely thing
is that the one location that processed your request did not have any
other “handle it elsewhere” directives, and so a file was served from
the filesystem.

The config snippet you provided has no “fastcgi_pass” directives, but
it does have some “include” directives, so it is possible that there is
configuration in one of those files that affects this request.

Without those “include” files, it looks like “location /” would be the
one that would handle your request (whether it is /blog, /blog/, or
/blog/index.php); and that just says “serve it from the filesystem”.

What do I have to change to make this work properly ?

Tell nginx what you want it to do with requests for php files, if it
isn’t just “serve it from the filesystem”.

Good luck with it,

f

Francis D. [email protected]

http://wiki.nginx.org/WordPress<<

I’ll have a read through this again. The part that might work

Thanks to Jonathan M. and Steve H. for some helpful
comments. I’ve made some progress and probably a couple of steps
backwards as well. Bit more help could be useful. NGINX version:
1.4.5 (Ubuntu). Looks like fastcgi is broken. There isn’t a fix for
this until the back end of April. PHP-FPM is working and so I’ll have
to use that.

NGINX is serving up static HTML pages but I now find that WordPress
pages are being served as plain text. What I mean by that is that
NGINX serves up this page…

" <?php
/**

  • Front to the WordPress application. This file doesn’t do anything,
    but loads
  • wp-blog-header.php which does and tells WordPress to load the
    theme.
  • @package WordPress
    */

/**

  • Tells WordPress to load the WordPress theme and output it.
  • @var bool
    */
    define(‘WP_USE_THEMES’, true);

/** Loads the WordPress Environment and Template */
require( dirname( FILE ) . ‘/wp-blog-header.php’ ) "

This doesn’t look very healthy. For that reason access is blocked
just now. For the WP installation which are blog / journalism / music
I get the above text as a web page when I put the URL into a browser.
Obviously this has to change so that the whole of a WP installation
appears a web page. What do I have to change to make this work
properly ? In /etc/nginx I can see …

common fastcgi_params.dpkg-dist nginx.conf nginx.conf.dpkg-dist
sites-available uwsgi_params.dpkg-dist conf.d naxsi-ui.conf.1.4.1
nginx.conf~ proxy_params.dpkg-dist sites-enabled

I suppose that I have to configure some of these to correct tha above
issue. Not quite sure which ones. In common there are locations.conf
and wpcommon.conf. In conf.d there are blockips.conf fastcgi.conf
upstream.conf. Can’t think that fastcgi.conf is of any use to me. In
sites-available there are default.conf default.dpkg-dist
sleepypenguin.conf. Not sure about these. In sites-enabled there is
default.conf sleepypenguin.conf. Not sure about those.

My nginx.conf looks like this…

user www-data;
pid /run/nginx.pid;
worker_processes 4;

events {
worker_connections 1024;
}

http {
##
# Basic Settings
##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

server {
## Your website name goes here.
server_name slepypenguin.homelinux.org;
## Your only path reference.
root /var/www/;
## This should be in your http block
index index.php;

    ##
    # Logging Settings
    # This may need updating if you are using awstats
    #
    ##

log_format main '$remote_addr - $remote_user [$time_local]

'$status $body_bytes_sent “$http_referer” ’

‘"$http_user_agent" “$http_x_forwarded_for”’;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

gzip  on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-

javascript text/xml application/xml application/xml+rss
text/javascript;

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    location / {
            # This is cool because no php is touched for static
            # include the "?$args" part so non-default permalinks

doesn’t break when using query string
try_files $uri $uri/ /index.php?$args;
}

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }

    ##
    # nginx-naxsi config
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # Virtual Hosts Config
    ##
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;

}
}


Richard

On Mon, Mar 10, 2014 at 09:53:53PM +0000, Richard I. wrote:

Hi there,

Thanks for some helpful answers so far. I’m sure I’m neary there.
Just need a bit more help. I’m getting error 404 when I try to load a
Wordpress page.

You can make it easier for people to help you if you don’t force them
to guess what you are doing.

“I make this request” – you didn’t say, so I’ll assume
“/blog/index.php”

“I get this response” – you did say http 404

“I want this response” – presumably the php-processed output of one
specific file on your filesystem, but you didn’t say which one.

So, given that the request is /blog/index.php, which one of your six
location blocks will nginx use to process the request?

location = /favicon.ico {
location = /robots.txt {
location /var/www {
location ~ .php$ {
location ~ [^/].php(/|$) {
location ~* ^.+.(js|css|png|jpg|jpeg|gif|ico)$ {

The docs are at http://nginx.org/r/location

What is the configuration in that location that tells nginx how to
process the request?

Is it what you want it to be?

f

Francis D. [email protected]

Hi

Thanks for some helpful answers so far. I’m sure I’m neary there.
Just need a bit more help. I’m getting error 404 when I try to load a
Wordpress page. That’s better than the previous attempt where NGINX
was loading the PHP code from WP. Some progress. I can also load
static HTML pages from my web server. NGINX access.log and error.log
are working. The error.log says…

*1 open() “/usr/share/nginx/html/blog/index.php” failed (2: No such
file or directory),
*1 open() “/usr/share/nginx/html/blog/index.php” failed (2: No such
file or directory),

I’ve done a search around the net for this error. I get some web pages
that suggest removing the $uri/ in the nginx.conf file. I’ve tried
that but it doesn’t work. I was kind of hoping that someone out there
might be able to suggest what to add to the nginx.conf file or what to
take away. There is nothing in conf.d other than fastcgi_params.conf.
Nothing in sites-available or sites-enabled. So far I’ve not run ’ ln
-s sites-available sites-enabled’ . So, mostly something to do with
nginx.conf ? …

user www-data;
pid /run/nginx.pid;
worker_processes 4;

events {
worker_connections 1024;
multi_accept on;
}

http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 10 10;
types_hash_max_size 2048;
client_max_body_size 20000k;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
index index.html index.htm index.php;
upstream php {
server unix:/tmp/php-fpm.sock;
server 127.0.0.1:9000;
}

server {
server_name sleepypenguin;
root /var/www/;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip  on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-

javascript text/xml application/xml application/xml+rss
text/javascript;

location = /favicon.ico {
log_not_found off;
access_log off;
}

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    location /var/www {
            try_files $uri /blog/index.php?$args;
    }

    location ~ .php$ {
    root html;
    }

    #location @wp {
    #rewrite ^/files(.*) /wp-includes/ms-files.php?file=$1 last;
    #root html;

            location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(blog.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                    return 404;
            }
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME

/var/www/blog$fastcgi_script_name;
include fastcgi_params;

    rewrite ^(/[^/]+)?(/wp-.*) $2 last;
    rewrite ^(/[^/]+)?(/.*.php) $2 last;
    rewrite ^/(.*)$ /index.php?q=$1 last;
    }

location ~* ^.+.(js|css|png|jpg|jpeg|gif|ico)$ {
rewrite ^./files/(.(js|css|png|jpg|jpeg|gif|ico))$ /wp-
includes/ms-files.php?file=$1 last;
expires 24h;
log_not_found off;
}

    rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
    }

   include /etc/nginx/conf.d/*.conf;
   include /etc/nginx/sites-enabled/*;

}


Richard
www.sheflug.org.uk

https://twitter.com/SleepyPenguin1