Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Nginx as a front-end proxy
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Nginx as a front-end proxy

chinmoychinmoy Member
edited February 2013 in Help

I was following this tutorial https://www.digitalocean.com/community/articles/how-to-configure-nginx-as-a-front-end-proxy-for-apache to have a setup where nginx handles static stuff and all php files ares handled by apache on a fresh box.
Everything went smoothly and I installed a php script on the server.

However, it is not loading any .html files. All .php files are loading and working fine. Whenever browser requests a html file, the browser kind of redirects back to index.php (although the url ending with .html remains in the address bar)

Could I have some help with this? :)

Comments

  • What are you seeing in the apache logs?

  • This is the apache error log:

    [Tue Feb 19 14:13:43 2013] [notice] Apache/2.2.22 (Ubuntu) configured -- resuming normal operations

    [Tue Feb 19 14:14:01 2013] [notice] caught SIGTERM, shutting down
    [Tue Feb 19 14:14:02 2013] [notice] Apache/2.2.22 (Ubuntu) configured -- resuming normal operations
    [Tue Feb 19 14:17:36 2013] [notice] SIGUSR1 received. Doing graceful restart
    apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
    [Tue Feb 19 14:17:36 2013] [notice] Apache/2.2.22 (Ubuntu) configured -- resuming normal operations
    [Tue Feb 19 14:19:23 2013] [notice] caught SIGTERM, shutting down
    [Tue Feb 19 14:19:24 2013] [notice] Apache/2.2.22 (Ubuntu) configured -- resuming normal operations
    [Tue Feb 19 14:28:40 2013] [notice] caught SIGTERM, shutting down
    [Tue Feb 19 14:28:41 2013] [notice] Apache/2.2.22 (Ubuntu) configured -- resuming normal operations
    [Tue Feb 19 14:31:12 2013] [notice] caught SIGTERM, shutting down
    [Tue Feb 19 14:31:16 2013] [notice] Apache/2.2.22 (Ubuntu) configured -- resuming normal operations
    [Tue Feb 19 14:31:23 2013] [notice] caught SIGTERM, shutting down
    [Tue Feb 19 14:31:24 2013] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3 with Suhosin-Patch configured -- resuming normal operations
    [Tue Feb 19 14:32:10 2013] [notice] caught SIGTERM, shutting down
    [Tue Feb 19 14:32:11 2013] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3 with Suhosin-Patch configured -- resuming normal operations
    [Tue Feb 19 14:32:20 2013] [error] [client 127.0.0.1] PHP Warning: mysql_connect(): Access denied for user 'www-data'@'localhost' (using password: NO) in /$
    [Tue Feb 19 15:04:24 2013] [notice] caught SIGTERM, shutting down
    [Tue Feb 19 15:04:25 2013] [notice] Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3 with Suhosin-Patch configured -- resuming normal operations

  • Nginx error logs here:

    http://d.pr/f/voxs

  • Paste your nginx conf here

  • change example.com in server_name example.com; to your domian in nginx conf file

  • @praveenbhat Changed example.com to my server ip. didn't work. Here is the conf file:

    server {
    listen 80;

        root /var/www/; 
        index index.php index.html index.htm;
    
        server_name my ip address; 
    
        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 127.0.0.1:8080;
    
         }
    
         location ~ /\.ht {
                deny all;
        }
    

    }

  • I looked at the tutorial, and its only forwarding the request to apache for any file end with .php look at:

    location ~ .php$ {

    you can try:

    location / {
    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:8080;

         }
    

    it will simply pass everything to apache on port 8080

    or you can play arround with passing php to vps1, js/images/all other static file to vps2 etc.

  • if I am not wrong, server_name should be a qualified domain name.....

    http://nginx.org/en/docs/http/server_names.html

  • EllimistEllimist Member
    edited February 2013

    Your configuration sends requests for non-existent files to index.php. Are you sure the html files you are requesting for exist?

  • The html files I'm requesting from the browser are generated by the php script. Seems like nginx can't read them or something and falling back to index.php. When I comment out the rule to fall back to index.php I get 404 on .html pages while the .php pages work fine.

  • look at your nginx error, this :

    2013/02/19 16:53:12 [error] 6558#0: *2 open() "/var/www/img-5123adf1e4dce.html" failed (2: No such file or directory)

    means that nginx looking for html file on /var/www folder, and not found.

    in your nginx conf, you set the root folder /var/www that's why nginx looking html file there, or it simply not forwarding any request end with .html to apache.

    comment out the original location / { .....

    and try:

    location / {
    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:8080;
    }

  • EllimistEllimist Member
    edited February 2013

    @graca said: and try:

    location / {

    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:8080;
    }

    Doesn't this defeat the purpose of having Nginx at the front?

    @chinmoy said: The html files I'm requesting from the browser are generated by the php script.

    In that case, you need to send requests for html files too to Apache.

     location ~* \.(php|html?)$ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass 127.0.0.1:8080;
    }
    
  • Actually I'm not sure what @chinmoy want to achieve, nginx configuration is different compare to apache, there are many ways to achieve the same goal in nginx.

    to simplify things, at the beginning just forward everything to apache, then from there, it can be customized as needed. the web is not just .php or .html but it can be .jpg .gif .css .js and many more.

  • "Changed example.com to my server ip. didn't work. "

    Did you add your VPS's IP address to the mod_rpaf config in apache?

    The config files we use in Debian Squeeze for rpaf.conf, nginx.conf, yoursite.com.conf, default:

    http://pastebin.com/nP4XdTzj

Sign In or Register to comment.