Want more timely Centmin Mod News Updates?
Become a Member

Nginx (Off-Topic) Certain Nginx Rewrite Rule Not Working

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by skringjer, Apr 28, 2019.

Tags:
  1. skringjer

    skringjer Member

    78
    11
    8
    Apr 21, 2019
    Ratings:
    +14
    Local Time:
    3:57 PM
    Please fill in any relevant information that applies to you:
    • CentOS Version: CentOS 7 64bit ?
    • Centmin Mod Version Installed: 123.09beta01.b135
    • Nginx Version Installed: 1.15.12
    • PHP Version Installed: 7.3.4
    • MariaDB MySQL Version Installed: 10.3.14
    • When was last time updated Centmin Mod code base ? : Automatic Cron updates
    • Persistent Config:
      Code (Text):
      LETSENCRYPT_DETECT='y'
      VHOSTCTRL_CLOUDFLAREINC='y'
      
    Greetings everyone, i am not sure if its allowed to post off-topic issues here but i have the below rewrite rule for my API which isnt working, anyone has any idea? sorry i am not experienced with nginx.

    Code (Text):
     # handle requests to the API
        location ~ /api/v2/(.+)$ {
            if (!-e $request_filename) { rewrite ^/api/v2/(.*) /api/v2/index.php?_page_url=$1 last; }
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    

    I am getting 404 on the api URL the author of the script that i am using suggested these to some users but these fixes are also not working.

    Certain versions of Nginx handle rewrite rules slightly differently. To fix this, in your Nginx rewrite rules change this:
    Code (Text):
    #location / {
        if (!-e $request_filename) {
            rewrite ^/(.*) /index.php?_page_url=$1 last;
        }
    #}
    

    To this:
    Code (Text):
    location / {
        if (!-e $request_filename) {
            rewrite ^/(.*) /index.php?_page_url=$1 last;
        }
    }
    

    After the above updates, ensure you reload Nginx so the changes are enabled.
    Alternative
    Alternatively replace the entire API location section with the following:
    Code (Text):
    rewrite ^/api/v2/(.*)$ /api/v2/index.php?_page_url=$1 last;
    [CODEB]
     
  2. eva2000

    eva2000 Administrator Staff Member

    41,350
    9,279
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,236
    Local Time:
    8:57 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    Centmin Mod is provide as is, so short of scripted related bugs or issues, any further optimisation or tweaks i.e. nginx rules to the web stack components - nginx, php-fpm, mariadb mysql, csf firewall etc or web app specific configurations are left to the Centmin Mod user to deal with. So I do not provide any free support for such.

    However, Centmin Mod users are free to help each other out and ask questions or give answers on this community forum. My hopes are that this community forum evolves so that more veteran long time Centmin Mod users help new Centmin Mod users out :)

    You can check out these guides
     
  3. skringjer

    skringjer Member

    78
    11
    8
    Apr 21, 2019
    Ratings:
    +14
    Local Time:
    3:57 PM
    Thank you, you are already doing more than anyone would, And yes would love to see some experienced users here giving their time just as you do.
     
  4. steph40

    steph40 Premium Member Premium Member

    58
    13
    8
    Jan 28, 2019
    Ratings:
    +25
    Local Time:
    6:57 AM
    1.1.5
    mariadb 10
    I am using this for my api:

    Code (Text):
    location /api/ {
        index index.html index.php;
        try_files $uri $uri/ /api/index.php?$args;
    }
    


    Works for me.
     
  5. skringjer

    skringjer Member

    78
    11
    8
    Apr 21, 2019
    Ratings:
    +14
    Local Time:
    3:57 PM
    This is the rewrite rule that i am using for my API

    Code:
    if (!-e $request_filename) { rewrite ^/api/v2/(.*) /api/v2/index.php?_page_url=$1 last; }
    As you can see in the above example, but what i think is that its a really old one and may be it dosent works with newer nginx.
     
  6. skringjer

    skringjer Member

    78
    11
    8
    Apr 21, 2019
    Ratings:
    +14
    Local Time:
    3:57 PM
    I think the order of the location block is important as suggested by the author of the script, i tried adding the api location block also in php-mycustom.conf at the top but its not working no matter where i put, the

    location block is right but i cant diagnose why it isnt working.
     
  7. skringjer

    skringjer Member

    78
    11
    8
    Apr 21, 2019
    Ratings:
    +14
    Local Time:
    3:57 PM
    Any help would really be appreciated, i hired a freelancer and he said the rewrite rule is perfectly correct but he couldnt diagnose why isnt it working.
     
  8. eva2000

    eva2000 Administrator Staff Member

    41,350
    9,279
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,236
    Local Time:
    8:57 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    you will have a conflict as nginx vhosts have their own /usr/local/nginx/conf/php.conf include file at bottom that matches *.php requests so probably it's catching your api requests so instead of using just /usr/local/nginx/conf/php.conf you can make a copy customised for your nginx vhost needs at /usr/local/nginx/conf/php-api.conf
    Code (Text):
    cp -a /usr/local/nginx/conf/php.conf /usr/local/nginx/conf/php-api.conf

    then modifying the if (!-e $request_filename) like in /usr/local/nginx/conf/php-api.conf to your version and change your reference in nginx vhost

    from
    Code (Text):
     # handle requests to the API
       location ~ /api/v2/(.+)$ {
           if (!-e $request_filename) { rewrite ^/api/v2/(.*) /api/v2/index.php?_page_url=$1 last; }
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
    

    to
    Code (Text):
     # handle requests to the API
       location ~ /api/v2/(.+)$ {
           include /usr/local/nginx/conf/php-api.conf;
       }
    

    see if that works
     
  9. skringjer

    skringjer Member

    78
    11
    8
    Apr 21, 2019
    Ratings:
    +14
    Local Time:
    3:57 PM
    Thank you @eva2000 for replying, just to clear my confusion here, i have replaced /usr/local/nginx/conf/php.conf in my nginx vhost with /usr/local/nginx/conf/php-mycustom.conf earlier for my changes and the contents of it are
    Code:
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            if (!-e $request_filename) { rewrite ^/(.*) /index.php?_page_url=$1 last; }
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param MOD_X_ACCEL_REDIRECT_ENABLED on;
        
    
    # might shave 200+ ms off PHP requests
    # which don't pass on a content length header
    # slightly faster page response time at the
    # expense of throughput / scalability
    #sendfile on;
    #tcp_nopush off;
    #keepalive_requests 0;
    
    fastcgi_connect_timeout 3000s;
    fastcgi_send_timeout 3000s;
    fastcgi_read_timeout 3000s;
    fastcgi_buffer_size 512k;
    fastcgi_buffers 512 16k;
    fastcgi_busy_buffers_size 1m;
    fastcgi_temp_file_write_size 4m;
    fastcgi_max_temp_file_size 4m;
    fastcgi_intercept_errors off;
    send_timeout 3000s;
    proxy_connect_timeout 3000s;
    proxy_send_timeout  3000s;
    proxy_read_timeout  3000s;
    client_max_body_size 0;
    
    # next 3 lines when uncommented / enabled
    # allow Nginx to handle uploads which then
    # passes back the completed upload to PHP
    #fastcgi_pass_request_body off;
    #client_body_in_file_only clean;
    #fastcgi_param  REQUEST_BODY_FILE  $request_body_file;
    
    #new .04+ map method
    fastcgi_param HTTPS $server_https;
    
    # comment out PATH_TRANSLATED line if /usr/local/lib/php.ini sets following:
    # cgi.fix_pathinfo=0
    # as of centminmod v1.2.3-eva2000.01 default is set to cgi.fix_pathinfo=1
    
    fastcgi_param  PATH_INFO          $fastcgi_path_info;
    fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
    
    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;
    
    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  REQUEST_SCHEME     $scheme;
    fastcgi_param  HTTPS              $https if_not_empty;
    fastcgi_param  HTTP_PROXY         "";
    
    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    
    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;
    
    # Set php-fpm geoip variables
    fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
    fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
    fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
    fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
    fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
    fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
    fastcgi_param GEOIP_REGION $geoip_region;
    fastcgi_param GEOIP_CITY $geoip_city;
    fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
    fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
    fastcgi_param GEOIP_LATITUDE $geoip_latitude;
    fastcgi_param GEOIP_LONGITUDE $geoip_longitude;
    
    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;
    
                       }
    Now about what you said then modifying the if (!-e $request_filename) like in /usr/local/nginx/conf/php-api.conf to your version and change your reference in nginx vhost

    Sorry but i am a bit confused here, Should i put the rewrite rule in php-mycustom.conf ? really sorry i didnt get you.
     
  10. skringjer

    skringjer Member

    78
    11
    8
    Apr 21, 2019
    Ratings:
    +14
    Local Time:
    3:57 PM
    i think i have to put this in php-api.conf file

    Code:
     # handle requests to the API
       location ~ /api/v2/(.+)$ {
          if (!-e $request_filename) { rewrite ^/api/v2/(.*) /api/v2/index.php?_page_url=$1 last; }
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
          include        fastcgi_params;
       }
    and then in my nginx vhost i have to put this right
    Code:
    # handle requests to the API
       location ~ /api/v2/(.+)$ {
          include /usr/local/nginx/conf/php-api.conf;
       }
     
  11. eva2000

    eva2000 Administrator Staff Member

    41,350
    9,279
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,236
    Local Time:
    8:57 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    Yes that's it
     
  12. skringjer

    skringjer Member

    78
    11
    8
    Apr 21, 2019
    Ratings:
    +14
    Local Time:
    3:57 PM
    @eva2000 sorry disturbing, but i tried putting this in /usr/local/nginx/conf/php-mycustom.conf and it didnt work

    Code:
     # handle requests to the API
       location ~ /api/v2/(.+)$ {
           if (!-e $request_filename) { rewrite ^/api/v2/(.*) /api/v2/index.php?_page_url=$1 last; }
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
    Then as you said above and from what i can understand from your above reply, i created a new file /usr/local/nginx/conf/php-api.conf and in that file i put
    Code:
     # handle requests to the API
       location ~ /api/v2/(.+)$ {
          if (!-e $request_filename) { rewrite ^/api/v2/(.*) /api/v2/index.php?_page_url=$1 last; }
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
          include        fastcgi_params;
       }
    And then in my nginx vhost file i put this
    Code:
    # handle requests to the API
       location ~ /api/v2/(.+)$ {
          include /usr/local/nginx/conf/php-api.conf;
       }
    And it didnt work, am i doing what you are suggesting?
     
  13. eva2000

    eva2000 Administrator Staff Member

    41,350
    9,279
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,236
    Local Time:
    8:57 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    in nginx vhost yourdomain.com.ssl.conf change from

    from
    Code (Text):
     # handle requests to the API
       location ~ /api/v2/(.+)$ {
           if (!-e $request_filename) { rewrite ^/api/v2/(.*) /api/v2/index.php?_page_url=$1 last; }
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
    

    to
    Code (Text):
     # handle requests to the API
       location ~ /api/v2/(.+)$ {
           include /usr/local/nginx/conf/php-api.conf;
       }
    

    make new copy of /usr/local/nginx/conf/php-api.conf
    Code (Text):
    cp -af /usr/local/nginx/conf/php.conf /usr/local/nginx/conf/php-api.conf

    edit just top part of the file from
    Code (Text):
    location ~ [^/]\.php(/|$) {
    include /usr/local/nginx/conf/503include-only.conf;
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
    return 404;
    }
    fastcgi_keep_conn on;
    fastcgi_pass dft_php;
    #fastcgi_pass 127.0.0.1:9000;
    #fastcgi_pass unix:/tmp/php5-fpm.sock;
    fastcgi_index index.php;
    #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    #fastcgi_param PHP_ADMIN_VALUE open_basedir=$document_root/:/usr/local/lib/php/:/tmp/;
    

    to (just top part of the file )
    Code (Text):
    location ~ [^/]\.php(/|$) {
    include /usr/local/nginx/conf/503include-only.conf;
    if (!-e $request_filename) { rewrite ^/api/v2/(.*) /api/v2/index.php?_page_url=$1 last; }
    fastcgi_keep_conn on;
    fastcgi_pass dft_php;
    #fastcgi_pass 127.0.0.1:9000;
    #fastcgi_pass unix:/tmp/php5-fpm.sock;
    fastcgi_index index.php;
    #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    #fastcgi_param PHP_ADMIN_VALUE open_basedir=$document_root/:/usr/local/lib/php/:/tmp/;
    

    restart both nginx and php-fpm services as you are editing both nginx/php config
    Code (Text):
    nprestart

    if that doesn't work it could be because your api isn't with .php extension ? does your api files in /api/v2 have .php extension ? if they do maybe try (just top part of the file of php-api.conf) change to
    Code (Text):
    location ~ [^/api/v2/]\.php(/|$) {
    include /usr/local/nginx/conf/503include-only.conf;
    
    if (!-e $request_filename) { rewrite ^/api/v2/(.*) /api/v2/index.php?_page_url=$1 last; }
    
    fastcgi_keep_conn on;
    fastcgi_pass dft_php;
    #fastcgi_pass 127.0.0.1:9000;
    #fastcgi_pass unix:/tmp/php5-fpm.sock;
    fastcgi_index index.php;
    #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    #fastcgi_param PHP_ADMIN_VALUE open_basedir=$document_root/:/usr/local/lib/php/:/tmp/; 
    


    or disregard my suggestions and try https://community.centminmod.com/th...nx-rewrite-rule-not-working.17331/#post-73267

    leave existing php.conf include intact and

    changing from
    Code (Text):
     # handle requests to the API
       location ~ /api/v2/(.+)$ {
           if (!-e $request_filename) { rewrite ^/api/v2/(.*) /api/v2/index.php?_page_url=$1 last; }
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
    

    to
    Code (Text):
    location ~ /api/v2/(.+)$ {
       try_files $uri $uri/ /api/v2/index.php?$args;
    }
    

    or
    Code (Text):
    location ~ /api/v2/(.+)$ {
       try_files $uri $uri/ /api/v2/index.php?_page_url=$1;
    }
    

    restart both nginx and php-fpm services as you are editing both nginx/php config
    Code (Text):
    nprestart


    Just some suggestions as I said Centmin Mod is provided as is so up to you to figure out how to configure your own custom web apps.
     
  14. eva2000

    eva2000 Administrator Staff Member

    41,350
    9,279
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,236
    Local Time:
    8:57 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    well @skringjer hired me privately to resolve this so came up with a tailored solution for his site/web app that works. So you should be all set :D
     
  15. skringjer

    skringjer Member

    78
    11
    8
    Apr 21, 2019
    Ratings:
    +14
    Local Time:
    3:57 PM
    <3 I couldn't ask for more. Everything is perfect now.
     
    • Like Like x 2