0

I have a NodeJS application running on port 2000 and a Wordpress installation on port 2010.

I have tried setting up my nginx.conf file to do the following:

  • domain.com: Serves the NodeJS application from port 2000
  • info.domain.com: Serves the Wordpress content

I have set the DNS settings of the info subdomain so it points to the same server as my NodeJS application.

The NodeJS application works fine. The Wordpress installation is acting really strange though.

The Wordpress urls have been wrong all along. I want them to look like http://info.domain.com/blah.../ but instead they look like this http://domain.com:2010/blah.../ — info.domain.com just seem to redirect.

I tried messing around with the Wordpress settings to see if they had some part in the problem above. Previously the Wordpress address and the site address were both http://domain.com:2010. I tried changing them to http://info.domain.com. That didn't work as the "info" part seemed to be stripped from my URL's leading to errors. To go back to the previous situation I changed them back using phpMyAdmin.

Now I have a whole new problem: http://domain.com:2010/somepage/ works as expected but when I try to access Wordpress' home page, either through http://domain.com:2010/ or through http://info.domain.com/ I am redirected to http://domain.com/ and the NodeJS application there.

I would really appreciate if someone could help me understand what goes wrong.

I am a beginner regarding server configuration and I am sure my nginx.conf file is one big messs. I have attached it below.

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

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    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;

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

    #include /etc/nginx/naxsi_core.rules;

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

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

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

    # server for reverse proxy on node application on port 2000
    server {
        listen          80;
        server_name     domain.com www.domain.com;

        #location /moodle {
        #   proxy_pass http://moodle.domain.com; 
        #   proxy_http_version 1.1;
        #   proxy_set_header Upgrade $http_upgrade;
        #   proxy_set_header Connection 'upgrade';
        #   proxy_set_header Host $host;
        #   proxy_cache_bypass $http_upgrade;
        #}

        location /old {
            rewrite ^ http://old.domain.com/ permanent;
        }

        location /moodle {
            rewrite ^ http://moodle.domain.com/ permanent;
        }

        location / {
            proxy_pass http://domain.com:2000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }

        location /info {
            proxy_pass http://domain.com:2010;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }

    # server for reverse proxy on node application on port 3000
    server {
        listen          80;
        server_name     beta.domain.com;

        location / {
            proxy_pass http://beta.domain.com:2000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }

    # Test re-routing to info.domain.com
    server {
        listen 80;
        server_name     info.domain.com;

        location / {
            proxy_pass http://domain.com:2010/;
        }
    }

    # server access for phpmyadmin to listen on port 81
    server {
        listen              81;
        server_name         localhost;
        root                /usr/share/phpmyadmin;
        index               index.php index.html index.htm;

        if (!-e $request_filename) {
            rewrite ^/(.+)$ /index.html?url=$1 last;
            break;
        }

        location ~ .php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include /etc/nginx/fastcgi_params;
        }
   }

   # server access for Wordpress to listen on port 2010
    server {
        listen              2010;
        server_name         info.domain.com;
        root                /home/take/wordpress;
        index               index.php index.html index.htm;
        client_max_body_size 256M;

        #if (!-e $request_filename) {
        #   rewrite ^/(.+)$ /index.html?url=$1 last;
        #   break;
        #}

        #location ~ .php$ {
        #   try_files $uri =404;
        #   fastcgi_pass unix:/var/run/php5-fpm.sock;
        #   fastcgi_index index.php;
        #   include /etc/nginx/fastcgi_params;
        #}



        # WordPress single site rules.
        # Designed to be included in any server {} block.

        # This order might seem weird - this is attempted to match last if rules below fail.
        # http://wiki.nginx.org/HttpCoreModule
        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        # Add trailing slash to */wp-admin requests.
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        # Directives to send expires headers and turn off 404 error logging.
        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
               access_log off; log_not_found off; expires max;
        }

        # Uncomment one of the lines below for the appropriate caching plugin (if used).
        #include global/wordpress-wp-super-cache.conf;
        #include global/wordpress-w3-total-cache.conf;

        # Pass all .php files onto a php-fpm/php-fcgi server.
        location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }
            # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)

            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #   fastcgi_intercept_errors on;
        #   fastcgi_pass php;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

   }




}

1 Answer 1

1

The reason as to why info.domain.com is redirecting you is because you're proxying it to domain.com. Remove the # Test re-routing to info.domain.com.

Your info.domain.com block should look as follows (listen 80; is the change):

# server access for Wordpress to listen on port 80
server {
    listen              80;
    server_name         info.domain.com;
    root                /home/take/wordpress;
    index               index.php index.html index.htm;
    client_max_body_size 256M;

    #if (!-e $request_filename) {
    #   rewrite ^/(.+)$ /index.html?url=$1 last;
    #   break;
    #}

    #location ~ .php$ {
    #   try_files $uri =404;
    #   fastcgi_pass unix:/var/run/php5-fpm.sock;
    #   fastcgi_index index.php;
    #   include /etc/nginx/fastcgi_params;
    #}



    # WordPress single site rules.
    # Designed to be included in any server {} block.

    # This order might seem weird - this is attempted to match last if rules below fail.
    # http://wiki.nginx.org/HttpCoreModule
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    # Directives to send expires headers and turn off 404 error logging.
    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
           access_log off; log_not_found off; expires max;
    }

    # Uncomment one of the lines below for the appropriate caching plugin (if used).
    #include global/wordpress-wp-super-cache.conf;
    #include global/wordpress-w3-total-cache.conf;

    # Pass all .php files onto a php-fpm/php-fcgi server.
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }
        # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)

        include fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #   fastcgi_intercept_errors on;
    #   fastcgi_pass php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

Then, when you access domain.com/info, use a redirect instead of proxing the request. Change the /info location block to:

location /info {
    return 301 $scheme://info.domain.com$request_uri;
}

The explanation:

Usually, when you proxy_pass you're telling Nginx to act as a reverse proxy to a gateway interface (something like Gunicorn or uWSGI or fastCGI etc.). What you've done, is asked Nginx to bind to the same port that you're proxying to, which in theory would work but it's not practical.

Your Wordpress site is binding to a unix socket, and so in order to access it, you need to go via Nginx. When you make a request in your browser, say http://info.domain.com, it defaults to port 80. I assumed that you understood this, since you've proxied this request to port 2010, but you should just be serving your content straight from this block.

I hope all of this makes sense.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response. It makes a lot of sense. However my info.domain.com is still redirected to the domain.com site. However urls to specific files works fine (info.domain.com/someFile.php works fine, info.domain.com/someSubdir/ doesn't). Do you have any suggestions to why that is happening and what I can do about it?
That's strange, it shouldn't be redirecting. Have you restarted Nginx and cleared any caching mechanisms you might have? If you have, please post your updated configuration. When you say info.domain.com/someSubdir/ isn't working, what is the error you get and what was the result that you were expecting? Generally, Nginx doesn't serve directories, only files. If you set autoindex on;, you'll get a hierarchical structure for which you can browse, but this is not recommended.
I tried waiting a while and that worked. It must have been some chacing issue. Everything works as it should now. Thank you very much for your answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.