0

I've installed tomcat for one website and apache2 for another (wordpress). The problem happens with wordpress website. There is nginx for reverse proxy and here it is:

server {
        listen   80;

        root /var/www/html/;
        index index.php index.html index.htm;

        server_name test.example.com;

        location / {
          try_files $uri $uri/ /index.php;
        }

        location ~ \.php$ {

        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8081;

         }

         location ~ /\.ht {
                deny all;
        }
}
server {
  listen 80;
  server_name conf.example.com;
  location / {
    proxy_pass http://127.0.0.1:8080;
  }
}

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name batterykazakhstan.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

}

So tomcat works well. But Wordpress is always redirecting to the welcome page in the loop. Could you help with this problem? I read about it and tried the solution suggested by putting to functions.php at top the:

remove_filter('template_redirect', 'redirect_canonical');

But that didnt help..

EDIT1:

The port 8081 is going to tomcat server and opening the test.example.com which is the Wordpress site

The port 8080 going to conf.example.com and opening the tomcat server website which works ok.

EDIT 2: here is the apache config:

ServerAdmin webmaster@localhost DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

EDIT 3:

I've got only two .htaccess files located here:

/var/www/html/wp-content/uploads/wp-clone/.htaccess

with contents:

<Files>
        Order allow,deny
        Deny from all
        Satisfy all
</Files>

And one more here

/var/www/html/wp-content/plugins/akismet/.htaccess

With contents:

<IfModule !mod_authz_core.c>
        Order Deny,Allow
        Deny from all
</IfModule>

# Apache 2.4
<IfModule mod_authz_core.c>
        Require all denied
</IfModule>

# Akismet CSS and JS
<FilesMatch "^(form\.js|akismet\.js|akismet\.css)$">
        <IfModule !mod_authz_core.c>
                Allow from all
        </IfModule>

        <IfModule mod_authz_core.c>
                Require all granted
        </IfModule>
</FilesMatch>

# Akismet images
<FilesMatch "^logo-full-2x\.png$">
        <IfModule !mod_authz_core.c>
                Allow from all
        </IfModule>

        <IfModule mod_authz_core.c>
                Require all granted
        </IfModule>
</FilesMatch>

EDIT 4:

wp-admin is working fine, its only the website itself. The Wordpress Adress (URL) and Site address (URL) both set to the http://test.example.com

maximus
  • 145
  • 6
  • Please give us more information like what service is port 8080 and 8081 is listening. What is your expected result, e.g. `test.example.com` should be redirected to backend tomcat, etc. – Simon MC. Cheng Aug 16 '16 at 06:54
  • @SimonMC.Cheng I've just updated the question, please take a look – maximus Aug 16 '16 at 07:08
  • Thanks for the update, so your Tomcat server has opened two ports: 8080 and 8081? Just curious, I am wondering if you have opened port 8080 under Apache which enable PHP? – Simon MC. Cheng Aug 16 '16 at 07:13
  • Tomcat opened on 8080 . Not 8081. Will check though. And 8080 is apache2 yeah opened in ports.conf . – maximus Aug 16 '16 at 07:15
  • Please help to check, Apache and Tomcat cannot both listen to port 8080. Besides, can you update the question by posting the .htaccess file of your Wordpress? It should be located in the root folder of your WordPress, example content [here](https://codex.wordpress.org/htaccess) – Simon MC. Cheng Aug 16 '16 at 07:38
  • @SimonMC.Cheng please take a look on two more edits The tomcat is listening only for 8080 and apache2 for 8081 for sure. – maximus Aug 16 '16 at 08:11
  • thanks for the update. First, I think you need to update the location block for php from `location ~ \.php$` to `location ~ \.php` as suggested [here](https://www.nginx.com/resources/admin-guide/reverse-proxy/). Then, restart nginx to apply the change and test again. According to your test result, you might need to add back the .htaccess file to /var/www/html/wp-content with content "Basic WP" by referencing [this] (https://codex.wordpress.org/htaccess) – Simon MC. Cheng Aug 16 '16 at 08:32
  • Further security suggestion on .htaccess, you might reference this [link](http://www.wpexplorer.com/htaccess-wordpress-security/) – Simon MC. Cheng Aug 16 '16 at 08:32
  • Wow, it worked with php config change, thank you :) – maximus Aug 16 '16 at 10:04
  • @SimonMC.Cheng It would be great if you could answer this question so that I can accept it and other people could view it then. Thank you for the solution – maximus Aug 16 '16 at 10:17

1 Answers1

1

Let me summarize the solution here.

In this sample, our author is going to build a typical Nginx-Apache web infrastructure. Nginx will be used as a web proxy, redirecting the traffic to different back-end server. Here is a simple network diagram showing the relationship.

Network diagram

The author would like to redirect all PHP traffic to his back-end Apache web server but since we might have parameters behind the php extension, so a matching for ending in php will fail. There is the location block we have used instead:

location ~ \.php

This will match any URL with .php keyword which should be able for the author to execute backend PHP application (e.g. WordPress). For a fresh installation, we might also need to check if the .htaccess file is located at the root folder of WordPress.

Reference link: Nginx Reverse Proxy admin guide, WordPress htaccess file, htaccess WordPress Security

Simon MC. Cheng
  • 436
  • 2
  • 7