Issues with Apache+Nginx

Started by Oldiesmann, March 01, 2011, 10:39:58 PM

Previous topic - Next topic

Oldiesmann

After reading up on how using Nginx to serve static content can improve memory usage, site load times, etc., I decided to set it up on my VPS, and used the tutorial at http://www.whmsecurity.com/unixlinux-security/how-to-install-nginx-with-cpanel-apache-in-proxy-mode/ (only I used more recent versions of Nginx and PCRE). For the most part, it worked well - sites seemed to load faster and I was able to set things up within 30 minutes with very little downtime.

However, after generating the vhost.conf file and uncommenting things as per that tutorial, I ran into some weird issues:
It couldn't find the images and CSS files for the Citiez theme at http://cincymoves.oldiesmann.us (trying to access those files resulted in a 404 Not Found error)
The Curve theme on that site worked pretty well, but there was a weird gap at the top and it generated a 404 error for the facebook.png image in that theme as well

Thumbnails for the gallery on my Archie site stopped working (though this might be due to the fact that the images themselves are outside of public_html, but I didn't try experimenting to see if changing the root from /home/archie/public_html to /home/archie would fix that)

I also had some theme issues with my SMF test board.

Due to the problems, I've commented out the parts to make it server static images for now, but I would like to get this working. Does anyone have any suggestions?
Michael Eshom
Christian Metal Fans

Ensiferous

Try having a go at my primer: http://blog.martinfjordvald.com/2010/07/nginx-primer/

If that doesn't help you out then please provide your the relevant vhost config and relevant error_log entries.
My Latest Blog Post: Debugging Nginx Errors

Oldiesmann

I think I have it figured out now actually. It was setting the root directories for the subdomains to /home/user/public_html instead of /home/user/public_html/subdomain. If I fix the roots for those, it works fine.

However, I do have one other issue I need to work out before I can enable this.

For ages, I've had the gallery set to rewrite URLs to download images:

    RewriteCond %{THE_REQUEST} /d/([0-9]+)-([0-9]+)/([^/?]+)(\?.|\ .)
    RewriteCond %{REQUEST_URI} !/index\.php$
    RewriteRule .   /gallery/main.php?g2_view=core.DownloadItem&g2_itemId=%1&g2_serialNumber=%2&g2_fileName=%3   [QSA,L]


If it weren't for the fact that it would cause problems with images previously posted on the forum, I'd just disable that particular rule from the Gallery2 admin area, but I don't want to do that.

How do I get Nginx to handle that rule?

I can also run a MySQL query to replace all the old URLs with the longer ones, but that's a bit of a pain and I'm not good with regex so I'd rather not do that if I don't have to.
Michael Eshom
Christian Metal Fans

Something like that

In the location block (or server block if none), add:


rewrite ^/d/([0-9]+)-([0-9]+)/([^/?]+)(\?.|\ .) /gallery/main.php?g2_view=core.DownloadItem&g2_itemId=$1&g2_serialNumber=$2&g2_fileName=$3 last;


Ought to do it.

Oldiesmann

Thanks for the help Mark! I'll try it out and let you know if it works.
Michael Eshom
Christian Metal Fans

Oldiesmann

Seems to be working fine. Thanks again!
Michael Eshom
Christian Metal Fans

Something like that

Quote from: Oldiesmann on March 02, 2011, 05:36:46 PM
Seems to be working fine. Thanks again!

Isn't that easier than Apache, too?

I <3 Nginx.

Something like that

Oldiesmann, your archieboy20.jpg example worked fine for me.

Make sure to control-refresh the broken images to see if your browser has cached the 404 response (I know Chrome will do this).

Oldiesmann

Yeah, it's all working again now. I don't really know what happened with that. That's why I ended up deleting that post and marking this solved again.
Michael Eshom
Christian Metal Fans

Oldiesmann

Hate to bump an old topic, but I have a quick question relating to this. I see that PHP 5.3.6 was just released, and I'd like to upgrade to that version. Can I just update it through WHM like usual, or are there extra steps I need to take first?
Michael Eshom
Christian Metal Fans

Something like that

I can't guarantee it, but if you don't let the upgrade replace the config file, you should be fine.

Oldiesmann

Thanks for the info Mark. I'll see if there's a script I can run to upgrade PHP without touching Apache.
Michael Eshom
Christian Metal Fans

Oldiesmann

#12
Hate to bump this again, but I'm having problems here. I just set this up on a new server, and can't get the rewrite rule to work at all.

I have tried putting it inside the server block, inside the "location /" block and inside the other location block, and no matter what I do, it won't work.

I've commented out the lines for the servers in question right now as I don't want to leave them with a broken gallery.

I also checked the vhost.conf file from the old server and it looks like I had given up on getting it to work there as well, as the location lines are commented out for the sites that have galleries.
Michael Eshom
Christian Metal Fans

Something like that

Can you give me example URL that aren't working, what they're supposed to be change to, and a copy of your entire server block?

Oldiesmann

I have the location blocks in these server blocks commented out at the moment, but here you go.

URL not working: http://www.archiefans.com/gallery/d/18285-2/untitled.JPG
What it should be changed to:  http://www.archiefans.com/gallery/main.php?g2_view=core.DownloadItem&g2_itemId=18285&g2_serialNumber=2&g2_fileName=untitled.JPG

I think that's right anyway - I'm not entirely sure what the g2_serialNumber item is used for.

The original rewrite rules from .htaccess for this URL:
    RewriteCond %{THE_REQUEST} /d/([0-9]+)-([0-9]+)/([^/?]+)(\?.|\ .)
    RewriteCond %{REQUEST_URI} !/index\.php$
    RewriteRule .   /gallery/main.php?g2_view=core.DownloadItem&g2_itemId=%1&g2_serialNumber=%2&g2_fileName=%3   [QSA,L]


It doesn't work with the rule you gave me above, or with the rule from http://gallery.menalto.com/node/80981

Server block:
   server {
  access_log off;

  error_log  logs/vhost-error_log warn;
  listen    80;
  server_name  archiefans.com www.archiefans.com;

  # uncomment location below to make nginx serve static files instead of Apache
  # NOTE this will cause issues with bandwidth accounting as files wont be logged
#  location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|js|css)$ {
#  root   /home/archie/public_html;
  #}

  location / {
   rewrite ^/d/([0-9]+)-([0-9]+)/([^/?]+)(\?.|\ .) /gallery/main.php?g2_view=core.DownloadItem&g2_itemId=$1&g2_serialNumber=$2&g2_fileName=$3 last;
   proxy_send_timeout   90;
   proxy_read_timeout   90;

   proxy_buffer_size    4k;
   # you can increase proxy_buffers here to suppress "an upstream response
   #  is buffered to a temporary file" warning
   proxy_buffers     16 32k;
   proxy_busy_buffers_size 64k;
   proxy_temp_file_write_size 64k;

   proxy_connect_timeout 30s;

   proxy_redirect  http://www.archiefans.com:81   http://www.archiefans.com;
   proxy_redirect  http://archiefans.com:81   http://archiefans.com;

   proxy_pass   http://69.194.199.74:81/;

   proxy_set_header   Host   $host;
   proxy_set_header   X-Real-IP  $remote_addr;
   proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}
Michael Eshom
Christian Metal Fans

Advertisement: