nginx rewrite rules instead Apache mod_rewrite .htacess

Discussion in 'aMember Pro v.4' started by giorgino, Nov 20, 2011.

  1. giorgino

    giorgino Member

    Joined:
    Jul 25, 2011
    Messages:
    54
    We're using nginx web server.
    Do anyone know the rewrite rules for nginx web server?

    thanks
  2. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    You can find an example in /amember/.htaccess:
    # Sample Nginx rules configuration:
    # aMember rules (replace /amember to actual aMember Pro path from site root)
    #
    # location ~* ^/amember/.*\.(js|ico|gif|jpg|png|css|swf|csv)$ {}
    # location ~* ^/amember/setup/index.php$ { try_files not-existing-file @php; }
    # location ~* ^/amember/js.php { try_files not-exiting-file @php; }
    # location ~* ^/amember/index.php$ { try_files not-existing-file @php; }
    # location ~* ^/amember/public.php$ { try_files not-existing-file @php; }
    #
    # location ~* ^/amember/public { rewrite ^.*$ /amember/public.php; }
    # location ~* ^/amember/setup { rewrite ^.*$ /amember/setup/index.php; }
    # location ~* ^/amember { rewrite ^.*$ /amember/index.php; }
    #
    # you must already have something like that in your nginx configuration:
    #
    # location ~ \.php$ {
    # try_files not-existing-file @php;
    # }
    # location @php {
    # fastcgi_pass 127.0.0.1:9000;
    # include fastcgi_params;
    # }
    #
    # it is necessary to have @php section, as it is referred by aMember rules, and
    # it must be located at bottom
    #
    #
  3. giorgino

    giorgino Member

    Joined:
    Jul 25, 2011
    Messages:
    54
    Thank you Alex, I found it, but don't work for me... (internal server error).
    Do you've some suggestion?
  4. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
  5. giorgino

    giorgino Member

    Joined:
    Jul 25, 2011
    Messages:
    54
    Thank you Alex, I've just sent to you the nginx configuration
  6. JamesAB

    JamesAB New Member

    Joined:
    Jan 16, 2005
    Messages:
    23
    I'm having the same problem. Can somebody post a working nginx config file?

    Here's what I have now:
    Code:
    server {
            listen  xxx.xxx.xxx.xxx:80;
            server_name test2.mysite.com;
            access_log    /var/log/nginx/access.log main;
            root    /home/mysite/public_html;
    
          location ~* ^/amember4/.*\.(js|ico|gif|jpg|png|css|swf|csv)$ {}
          location ~* ^/amember4/setup/index.php$ { try_files not-existing-file @php; }
          location ~* ^/amember4/js.php { try_files not-exiting-file @php; }
          location ~* ^/amember4/index.php$ { try_files not-existing-file @php; }
          location ~* ^/amember4/public.php$ { try_files not-existing-file @php; }
    
          location ~* ^/amember4/public { rewrite ^.*$ /amember4/public.php; }
          location ~* ^/amember4/setup { rewrite ^.*$ /amember4/setup/index.php; }
          location ~* ^/amember4 { rewrite ^.*$ /amember4/index.php; }
    
          location ~ \.php$ {
              try_files not-existing-file @php;
          }
    
          location @php {
              fastcgi_pass 127.0.0.1:9000;
            include fastcgi_params;
          }
    
    }
    
    I get 500 errors on /amember4/index.php . Here's clue from the nginx error log.
    Code:
    2012/02/02 12:27:55 [error] 28780#0: *50 open() "/home/mysite/public_html/application/default/views/public/img/top.png" failed (2: No such file or directory), client: 64.222.159.2, server: test2.mysite.com, request: "GET /application/default/views/public/img/top.png HTTP/1.1", host: "test2.mysite.com", referrer: "http://test2.mysite.com/amember4/setup"
    
    These errors show it is looking the document root rather than the /amember4/ directory where the files are uploaded.
  7. wallpaper_01

    wallpaper_01 Member

    Joined:
    Mar 9, 2011
    Messages:
    116
    Did you fix this? Im having the same problem, except mine is looking in /usr/www/ and ive not even told it to! mine is /var/www/nginx-default
  8. JamesAB

    JamesAB New Member

    Joined:
    Jan 16, 2005
    Messages:
    23
    No. I never got it working.

    The errors weren't being logged on my test server. I haven't had time to go back and troubleshoot it myself yet.

    If anyone has a working nginx config, I'd love to see it.
  9. wallpaper_01

    wallpaper_01 Member

    Joined:
    Mar 9, 2011
    Messages:
    116
    Yeah I got it working yesterday: this is what I have below:

    Code:
    server {
            listen  80;
            server_name  elearning-mkupdate.co.uk;
            access_log  /var/log/nginx/localhost.access.log;
      
                    root  /var/www/nginx-default;
                    index  index.php index.html;
    
            client_max_body_size 50M;
            client_body_buffer_size 128k;
    
            location ~* ^/amember/.*\.(js|ico|gif|jpg|png|css|swf|csv)$ {}
            location ~* ^/amember/setup/index.php$ { try_files not-existing-file @php; }
            location ~* ^/amember/js.php { try_files not-exiting-file @php; }
            location ~* ^/amember/index.php$ { try_files not-existing-file @php; }
            location ~* ^/amember/public.php$ { try_files not-existing-file @php; }
    
            location ~* ^/amember/public { rewrite ^.*$ /amember/public.php; }
            location ~* ^/amember/setup { rewrite ^.*$ /amember/setup/index.php; }
            location ~* ^/amember { rewrite ^.*$ /amember/index.php; }
    
            #error_page  404  /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
    
            #error_page  500 502 503 504  /50x.html;
          # location = /50x.html {
          #        root  /var/www/nginx-default/;
          # }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
                    #proxy_pass  http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            location ~ \.php$ {
                    try_files not-existing-file @php;
            }
    
            location @php {
    
                    fastcgi_pass  127.0.0.1:9000;
                    include        /etc/nginx/fastcgi_params;
                    fastcgi_param  SCRIPT_FILENAME  /var/www/nginx-default/$fastcgi_script_name;
    
            }
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            location ~ /\.ht {
                    deny  all;
            }
    
            location /phpmyadmin {
                  root /usr/share/;
                  index index.php index.html index.htm;
                  location ~ ^/phpmyadmin/(.+\.php)$ {
                          try_files $uri =404;
                          root /usr/share/;
                          fastcgi_pass 127.0.0.1:9000;
                          fastcgi_index index.php;
                          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                          include /etc/nginx/fastcgi_params;
                  }
                  location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                          root /usr/share/;
                  }
            }
            location /phpMyAdmin {
                  rewrite ^/* /phpmyadmin last;
            }
     
    }
    jimp3bd likes this.
  10. th3gov

    th3gov aMember Pro Customer

    Joined:
    Aug 8, 2012
    Messages:
    14
    I tried the same rules on nginx, but I still receive "File not found.".
    I changed only the directory from "amember" to "members".


    Please help :/
  11. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Please attach exact config which you have.
  12. th3gov

    th3gov aMember Pro Customer

    Joined:
    Aug 8, 2012
    Messages:
    14
    I fixed the issue by myself. I have forgot to change the fastcgi_param SCRIPT_FILENAME directory.

    Sorry!

Share This Page