• I have set up my nginx config as recommended at http://codex.wordpress.org/Nginx and it all works fine. Now, I want to add a reverse proxy so that mysite.com/logs/ points to port 8000 on another local machine (192.168.0.105:8000). In my attempt to accomplish this, I added the following code to the bottom of /etc/nginx/global/wordpress.conf :

    location /logs/ {
    proxy_pass http://192.168.0.105:8000;
    proxy_redirect default;
    proxy_set_header Host $http_host;
    }

    The desired effect, however, is not accomplished. Pointing a browser to mysite.com/logs/ now takes me to a wordpress 404 page. Strangely, I first get the popup from 192.168.0.105:8000 asking for my kibana credentials, but upon entering them I am redirected to the 404 page. I believe my reverse proxy is being negated somewhere else in the config file, I just don’t know nginx well enough to figure out how to fix this. Any thoughts? Below is my nginx config:

    **********nginx.conf

    # Generic startup file.
    user nginx nginx;
    
    #usually equal to number of CPUs you have. run command "grep processor /proc/cpuinfo | wc -l" to find it
    worker_processes 2;
    
    error_log /www/mysite.com/logs/error.log;
    pid /var/run/nginx.pid;
    
    # Keeps the logs free of messages about not being able to bind().
    #daemon off;
    
    events {
    worker_connections 1024;
    }
    
    http {
    # rewrite_log on;
    
    include mime.types;
    default_type application/octet-stream;
    access_log /www/mysite.com/logs/access.log;
    sendfile on;
    # tcp_nopush on;
    keepalive_timeout 3;
    # tcp_nodelay on;
    # gzip on;
    #php max upload limit cannot be larger than this
    client_max_body_size 13m;
    index index.php index.html index.htm;
    
    # Upstream to abstract backend connection(s) for PHP.
    upstream php {
    #this should match value of "listen" directive in php-fpm pool
    server unix:/var/run/php-fpm/www.sock;
    server 127.0.0.1:9000;
    }
    
    include sites-enabled/*;
    include conf.d/*.conf;
    }

    **********/sites-available/mysite.conf

    # Redirect everything to the main site. We use a separate server statement and NOT an if statement - see http://wiki.nginx.org/IfIsEvil
    
    server {
    server_name _;
    rewrite ^ $scheme://mysite.com$request_uri redirect;
    }
    
    server {
    server_name mysite.com;
    root /www/mysite.com/public;
    
    index index.php;
    
    include global/restrictions.conf;
    
    # Additional rules go here.
    
    # Only include one of the files below.
    include global/wordpress.conf;
    # include global/wordpress-ms-subdir.conf;
    # include global/wordpress-ms-subdomain.conf;
    }

    **********/global/wordpress.conf

    # 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_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_index index.php;
    # fastcgi_intercept_errors on;
    fastcgi_pass php;
    }
    
    location /logs/ {
    proxy_pass http://192.168.0.105:8000;
    proxy_redirect default;
    proxy_set_header Host $http_host;
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter lonhig

    (@lonhig)

    No nginx people here? I also posted this on the nginx support forum, though I received no reply there either. Isn’t this something that should be really easy? Their documentation is not getting me through it, and neither are my nginx logs. Maybe I should just start over with apache?

    Dion

    (@diondesigns)

    WordPress relies so heavily on .htaccess files that using anything other than Apache is bound to cause problems.

    Side comment. Until nginx supports .htaccess files, it will remain on the outside looking in. And the window is closing fast — the main historical benefit of nginx over Apache has been almost completely negated with the latest versions of Apache.

    When you say proxy I suspect that you mean redirection. When you access page: http://mysite.com/logs/ you want the browser to access and display a page from a different site, in this case: (192.168.0.105:8000)
    Is this correct ?

    If it is then I see several options:
    A) This plugin: https://wordpress.org/plugins/quick-pagepost-redirect-plugin/
    B) Add code to the “.htaccess” file (or nginx equivalent)
    C) Add a custom page template to your theme (hopefully a child theme) which in it code a header which does the redirect.

    Otherwise I googled on: “wordpress page redirect to another site” and got many options.

    Thread Starter lonhig

    (@lonhig)

    You are correct in that I want /logs/ to redirect to a different site. However, a reverse proxy provides one benefit that simple redirection does not: I want to hide the location (port) of the site being redirected to. I want to be able to access the service on the 8000 port without opening that port to the web. A reverse proxy makes this possible.

    I am willing to go through the giant pain of ditching my nginx setup and starting over with apache if someone can first tell me how to use apache to setup a reverse proxy alongside a wordpress install.

    Thread Starter lonhig

    (@lonhig)

    Actually the answer is probably to make an apache server for wordpress, then put both wordpress and my other services behind an nginx reverse proxy a la this guide: https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-14-04-droplet
    I’ll just try that barring any more input here.

    lonhig, you can absolutely do what you want.

    Move your location /logs/ directive before try_files. Also, remove the trailing slash for logs and add the URI to the pass:

    location ~* /logs {
    proxy_pass http://192.168.0.105:8000;
    }

    The tilde + asterisk will make the match case-insensitive. Remember, Nginx removes the matching portion in the handoff, so if you the URL for Kibana is 192.168.0.105:8000/logs/, you will have to add it back in on the pass:

    location ~* /logs {
    proxy_pass http://192.168.0.105:8000/logs/;
    }

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘How do I set up a reverse proxy with nginx?’ is closed to new replies.