Skip to main content
Topic: rewrite rules for nginx deployment (Read 3484 times) previous topic - next topic

rewrite rules for nginx deployment

Greetings,

I am curious if anyone has used phprad with Nginx and how they configured the rewrite rules to adjust for the .htaccess file?

I am getting a 405 when trying to POST login information after publishing the site to a nginx server.

Here are the htaccess rewrites:
RewriteEngine on
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request_uri=$1 [NC,L,QSA,B]

Since they are not observed by nginx, here are the rewrites I am trying in the nginx config:
   location / {
  if (!-e $request_filename){
    rewrite ^(.*)$ /index.php?request_uri=$1 break;
  }
}

Thank you!

 

Re: rewrite rules for nginx deployment

Reply #1
Haven't tested it yet, but try this:

# nginx for phprad
autoindex off;
autoindex off;
location / {
  if (!-e $request_filename){
    rewrite ^(.*)$ /index.php?request_uri=$1 break;
  }
}

Re: rewrite rules for nginx deployment

Reply #2
That was the original code from the rewrite converter tool I used - I tried that, got the same results...

Does anyone have a working phprad deployment on an Nginx server?

I may have to find an alternate development platform if it has specific dependencies on external software like apache.

Re: rewrite rules for nginx deployment

Reply #3
@persiusone‍ add this to your server .conf file
Code: [Select]
   
    autoindex off;

    location / {
        try_files $uri $uri.html $uri/ @hide-php-ext /index.php?$query_string;
        index index.html index.htm index.php;
    }

    location @hide-php-ext {
        rewrite ^(.*)$ $1.php last;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include nginx.fastcgi.conf;
        include nginx.redis.conf;
        fastcgi_pass php_farm;
fastcgi_hide_header X-Powered-By;
    }

Re: rewrite rules for nginx deployment

Reply #4
That works much better, thank you!

Re: rewrite rules for nginx deployment

Reply #5
@persiusone‍ add this to your server .conf file
Code: [Select]
   
    autoindex off;

    location / {
        try_files $uri $uri.html $uri/ @hide-php-ext /index.php?$query_string;
        index index.html index.htm index.php;
    }

    location @hide-php-ext {
        rewrite ^(.*)$ $1.php last;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include nginx.fastcgi.conf;
        include nginx.redis.conf;
        fastcgi_pass php_farm;
fastcgi_hide_header X-Powered-By;
    }



hi @willvin
I still can't get nginx to work with phprad. I leave my conf to see if they can help me.

Code: [Select]
server
{
    listen 80;
listen 443 ssl;
    server_name mysite.org;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/mysite.org;
   
    #SSL-START SSL related configuration, do NOT delete or modify the next line of commented-out 404 rules
    #error_page 404/404.html;
    limit_conn perserver 300;
    limit_conn perip 25;
    limit_rate 512k;
    #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
    #HTTP_TO_HTTPS_END
    ssl_certificate    /www/server/panel/vhost/cert/mysite.org/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/mysite.org/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;

    #SSL-END
   
    #ERROR-PAGE-START  Error page configuration, allowed to be commented, deleted or modified
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
   
    #PHP-INFO-START  PHP reference configuration, allowed to be commented, deleted or modified
    #SECURITY-START Hotlink protection configuration
    location ~ .*\.(jpg|jpeg|gif|png|js|css)$
    {
        expires      30d;
        access_log off;
        valid_referers none blocked mysite.org;
        if ($invalid_referer){
           return 404;
        }
    }
    #SECURITY-END
    include enable-php-74.conf;
    #PHP-INFO-END
   
    #REWRITE-START URL rewrite rule reference, any modification will invalidate the rewrite rules set by the panel
    include /www/server/panel/vhost/rewrite/mysite.org.conf;
    #REWRITE-END
   
    # Forbidden files or directories
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
   
    # Directory verification related settings for one-click application for SSL certificate
    location ~ \.well-known{
        allow all;
    }
   
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log off;
    }
   
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log off;
    }
    access_log  /www/wwwlogs/mysite.org.log;
    error_log  /www/wwwlogs/mysite.org.error.log;
}

Re: rewrite rules for nginx deployment

Reply #6
@ddiaz2380‍ it should look like this

Code: [Select]
server {
    listen 80;
listen 443 ssl;
    server_name mysite.org;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/mysite.org;
  
    #SSL-START SSL related configuration, do NOT delete or modify the next line of commented-out 404 rules
    #error_page 404/404.html;
    limit_conn perserver 300;
    limit_conn perip 25;
    limit_rate 512k;
    #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
    #HTTP_TO_HTTPS_END
    ssl_certificate    /www/server/panel/vhost/cert/mysite.org/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/mysite.org/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;

    #SSL-END
  
    #ERROR-PAGE-START  Error page configuration, allowed to be commented, deleted or modified
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
  
    #PHP-INFO-START  PHP reference configuration, allowed to be commented, deleted or modified
    #SECURITY-START Hotlink protection configuration
    location ~ .*\.(jpg|jpeg|gif|png|js|css)$
    {
        expires      30d;
        access_log off;
        valid_referers none blocked mysite.org;
        if ($invalid_referer){
           return 404;
        }
    }
    #SECURITY-END
    include enable-php-74.conf;
    #PHP-INFO-END
  
    #REWRITE-START URL rewrite rule reference, any modification will invalidate the rewrite rules set by the panel
    include /www/server/panel/vhost/rewrite/mysite.org.conf;
    #REWRITE-END
  
    # Forbidden files or directories
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
  
    # Directory verification related settings for one-click application for SSL certificate
    location ~ \.well-known{
        allow all;
    }
  
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log off;
    }
  
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log off;
    }
    access_log  /www/wwwlogs/mysite.org.log;
    error_log  /www/wwwlogs/mysite.org.error.log;

    ## the new config starts here.
    autoindex off;

    location / {
        try_files $uri $uri.html $uri/ @hide-php-ext /index.php?$query_string;
        index index.html index.htm index.php;
    }

    location @hide-php-ext {
        rewrite ^(.*)$ $1.php last;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include nginx.fastcgi.conf;
        include nginx.redis.conf;
        fastcgi_pass php_farm;
fastcgi_hide_header X-Powered-By;
    }
   ## the new config ends here
}

Re: rewrite rules for nginx deployment

Reply #7
@ddiaz2380‍ it should look like this

Code: [Select]
server {
    listen 80;
listen 443 ssl;
    server_name mysite.org;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/mysite.org;
  
    #SSL-START SSL related configuration, do NOT delete or modify the next line of commented-out 404 rules
    #error_page 404/404.html;
    limit_conn perserver 300;
    limit_conn perip 25;
    limit_rate 512k;
    #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
    #HTTP_TO_HTTPS_END
    ssl_certificate    /www/server/panel/vhost/cert/mysite.org/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/mysite.org/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;

    #SSL-END
  
    #ERROR-PAGE-START  Error page configuration, allowed to be commented, deleted or modified
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
  
    #PHP-INFO-START  PHP reference configuration, allowed to be commented, deleted or modified
    #SECURITY-START Hotlink protection configuration
    location ~ .*\.(jpg|jpeg|gif|png|js|css)$
    {
        expires      30d;
        access_log off;
        valid_referers none blocked mysite.org;
        if ($invalid_referer){
           return 404;
        }
    }
    #SECURITY-END
    include enable-php-74.conf;
    #PHP-INFO-END
  
    #REWRITE-START URL rewrite rule reference, any modification will invalidate the rewrite rules set by the panel
    include /www/server/panel/vhost/rewrite/mysite.org.conf;
    #REWRITE-END
  
    # Forbidden files or directories
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
  
    # Directory verification related settings for one-click application for SSL certificate
    location ~ \.well-known{
        allow all;
    }
  
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log off;
    }
  
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log off;
    }
    access_log  /www/wwwlogs/mysite.org.log;
    error_log  /www/wwwlogs/mysite.org.error.log;

    ## the new config starts here.
    autoindex off;

    location / {
        try_files $uri $uri.html $uri/ @hide-php-ext /index.php?$query_string;
        index index.html index.htm index.php;
    }

    location @hide-php-ext {
        rewrite ^(.*)$ $1.php last;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include nginx.fastcgi.conf;
        include nginx.redis.conf;
        fastcgi_pass php_farm;
fastcgi_hide_header X-Powered-By;
    }
   ## the new config ends here
}


hi @willvin
Still not working, we are using aapanel, with apache it works fine, but we would like to be able to run nginx, for the functions it provides. I pass him the nginx configuration from which he distributes each site that is created.


Code: [Select]
user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

http
    {
        include       mime.types;
#include luawaf.conf;

include proxy.conf;

        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;

server
    {
        listen 888;
        server_name phpmyadmin;
        index index.html index.htm index.php;
        root  /www/server/phpmyadmin;

        #error_page   404   /404.html;
        include enable-php.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /www/wwwlogs/access.log;
    }
include /www/server/panel/vhost/nginx/*.conf;
}



Re: rewrite rules for nginx deployment

Reply #9
@ddiaz2380‍ please contact your panel/server provider to give you info on how to add the config, thanks.

They are our servers, we install one aapanel so that they can add the sites created with phprad in a simple way. With apache it works fine, but with nginx, it doesn't run, adding any configuration turns off nginx and gives error.



Re: rewrite rules for nginx deployment

Reply #12
@ddiaz2380‍ Please contact  @persiusone‍ to show you how he added it and what you are doing wrong.

with apache and openlitespeed it works perfect, well I'll fix that later on in nginx. Another query, error 500 why it happens? it gives me in some applications when I take them to production.