3

I generally use Nginx to serve static content on my server, with Apache handling PHP content using PHP-FPM. However, I'm not able to get a Wordpress blog homepage to display and I've tried all the configuration examples I can find on the web without much luck.

Here is my Nginx config:

server {

        listen   XXX.XXX.XXX.XXX:80;
        server_name wptest.mydomain.com;

    access_log  /var/log/nginx/testblog_access.log combined;
    error_log /var/log/nginx/testblog-error.log;

    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    location / {
                    proxy_pass http://127.0.0.1:80;
    }

    location = /50x.html {
            root   /var/www/nginx-default;
    }

    # No access to .htaccess files.
    location ~ /\.ht {
            deny  all;
    }

}

My Apache config is as follows:

<VirtualHost 127.0.0.1>
    ServerName wptest.mydomain.com
    ServerAdmin [email protected]
    LogLevel warn
    ErrorLog /var/log/apache2/testblog-error.log
    CustomLog /var/log/apache2/testblog-access.log combined

    Options +FollowSymLinks +ExecCGI -Indexes -MultiViews
    AddHandler php-fastcgi .php
    Action php-fastcgi /wordpress
    Alias /wordpress /var/www/wordpress
    FastCgiExternalServer /var/www/wordpress -host 127.0.0.1:9000
    RewriteEngine on

    DocumentRoot /var/www/wordpress
    DirectoryIndex index.php

                    <Directory />
                            DirectoryIndex index.php
                            AllowOverride All
                            Options +FollowSymLinks +ExecCGI +SymLinksIfOwnerMatch
                    </Directory>

                    <Directory /var/www/wordpress>
                            AllowOverride All
                            Options +FollowSymLinks +ExecCGI +SymLinksIfOwnerMatch
                    </Directory>
</VirtualHost>

I am not able to view "http://wptest.mydomain.com/" or "http://wptest.mydomain.com/wp-admin" but "http://wptest.mydomain.com/wp-login.php" does work. What am I doing wrong?

Version information: + OS: Debian 5/Lenny + Apache: 2.2.9 + Nginx: 0.7.65 + Wordpress: 3.1.2

6
  • I can't test your configs right now, but the first thing that came into my mind, have you tried wptest.mydomain.com/index.php? Does that work? What 404 do you get? From Nginx or from Apache? Are you able to directly access your blog fine directly on Apache? Please update your question with answer to these. May 22, 2011 at 7:12
  • Trying to use wptest.mydomain.com/index.php results in a redirect to wptest.mydomain.com/ which triggers the 404. I'm seeing 404 errors in both nginx and apache logs so I'm not sure how to narrow that down further. As for accessing the blog directly via Apache, Apache serves many virtual hosts only the localhost IP, so I'm not sure how to check whether content is served correctly from the command-line.
    – avggeek
    May 22, 2011 at 8:14
  • Set wptest.mydomain.com to 127.0.0.1 in /etc/hosts and access it via lynx. Lynx will request it directly from apache instead of nginx, so you will know. May 22, 2011 at 8:26
  • Never thought of that - thanks! After setting wptest.mydomain.com to localhost, lynx is also showing a 404/Not Found Error. Any ideas on how to fix my Apache config?
    – avggeek
    May 23, 2011 at 6:16
  • can you please show your .htaccess from your wordpress root directory if you have one? or let me know if you don't have one. I now have a test machine and will check your apache config (which seems fine) and will let you know if I find something. May 23, 2011 at 6:52

3 Answers 3

2

Both servers are listening to the same port. You have Nginx set to listen to 80 and nothing is set for Apache unless it's in your ports.conf.

Your also proxy passing to Apache port 80 in your Nginx conf.

In the Nginx conf change

proxy_pass http://127.0.0.1:80;

to

proxy_pass http://127.0.0.1:9000;

change listen XXX.XXX.XXX.XXX:80; to listen 80;

In your Vhosts

add

NameVirtualHost *:9000
Listen 9000

Above the <VirtualHost> tag or in the ports.conf file (If you have other vhost that don't use Nginx add it to the top of your vhost. Change virtual host to look like this:

<VirtualHost *9000>

4
  • Chris my guess is Apache seems to be listening on localhost only, while nginx listening on the external IP. May 22, 2011 at 9:52
  • Thats what you want. Everything will publicly pass through Nginx then Nginx will either serve the static content or send it to Apache
    – Chris_O
    May 23, 2011 at 5:42
  • Chris - In ports.conf, I have Apache configured as follows: NameVirtualHost 127.0.0.1 Listen 127.0.0.1:80 So Apache is listening only on localhost and Nginx listens on the public IP. So there shouldn't be a conflict in terms of listening ports.
    – avggeek
    May 23, 2011 at 6:39
  • The port is different from the ip. The whole concept of a reverse proxy is to move Apache OFF the standard http port which is 80. Take a look at the settings I outlined in a tutorial on how to do this.
    – Chris_O
    May 23, 2011 at 8:59
0

I copied your config and I got those 404s. It seems like you have mixed up fastcgi and Apache is confused where to find fastcgi and WordPress. As soon as I separated the fastcgi binary from WordPress I was able to make it work. In my setup I had WordPress was installed in /var/www/wordpress which is out of document root, my document root was something out of /var. But I could access WordPress as http://domain.com/wordpress/ because of following Alias.

Alias /wordpress /var/www/wordpress

Then I created a fastcgi directory outside of WordPress in /var/www so it will be

mkdir /var/www/fastcgi

Then I linked the php5-cgi binary to this directory:

ln -s /usr/bin/php5-cgi /var/www/fastcgi/

I separated the php5-cgi because I don't like the idea of having a binary file under the directory accessible via web server.

Then I config my Apache as follows:

Alias /wordpress /var/www/wordpress
Alias /fastcgi   /var/www/fastcgi

FastCgiExternalServer /var/www/fastcgi -host 127.0.0.1:9000
<Directory /var/www/wordpress>
    AllowOverride All
    AddHandler php-fastcgi .php
    Action php-fastcgi /fastcgi/php5-cgi
    AddType application/x-httpd-php .php
    DirectoryIndex index.php

   Options +FollowSymLinks +ExecCGI +SymLinksIfOwnerMatch
</Directory>

<Location /fastcgi>
    SetHandler fcgid-script
    Options +ExecCGI
</Location>

And it started working. Hope this will help you fix your Apache config, let me know if there is anything wrong.

0

I don't know whether I should be proud or embarrassed that I'm coming back to answer my own question more than a year later. So, I finally found some time to work on this issue today and managed to get this configuration working. Turns out there were a few missing statements in my original configuration, but the critical change was in wp-config.php. To pull everything together, starting off with my nginx config (which is almost entirely lifted from the nginx wiki):

server {

        listen   XXX.XXX.XXX.XXX:80;
        server_name wptest.mydomain.com;
        error_log /var/log/nginx/wp-error.log;

        ## Your only path reference.
        root /var/www/wordpress;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        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 content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:80;
        }

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

}

Next up, my Apache config:

<VirtualHost 127.0.0.1>
        server_name wptest.mydomain.com
        ServerAdmin [email protected]
        LogLevel warn
        ErrorLog /var/log/apache2/wp-error.log
        CustomLog /var/log/apache2/wp-access.log combined

        AddType application/x-httpd-fastphp .php
        AddHandler php-fastcgi .php
        Action php-fastcgi /fastcgi
        Alias /fastcgi /var/www/wordpress
        FastCgiExternalServer /var/www/wordpress -host 127.0.0.1:9000

                        DocumentRoot /var/www/wordpress

                        #Site Directives
                        RewriteEngine on
                        Options +FollowSymLinks +ExecCGI -Indexes -MultiViews

                        <Directory />
                          AllowOverride None
                        </Directory>

                        <Directory /var/www/wordpress/>
                                AllowOverride Limit FileInfo AuthConfig
                                order allow,deny
                                allow from all
                        </Directory>

</VirtualHost>

Finally, the one extra line in the wp-config.php file that makes it all work (Taken from the Codex)

$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];

With this configuration, I have tested that pretty permalinks via .htaccess works correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.