We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Help me config rule website in Nginx

Hi, My project using phalcon 2.1 on Nginx server

  1. .htaccess on root public_html
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    RewriteRule ^templates - [L,NC]
    RewriteRule ^themes - [L,NC]
    RewriteRule  ^$ public/    [L]
    RewriteRule  (.*) public/$1 [L]
</IfModule>
  1. .htaccess on /public folder
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

I have use convert htaccess to nginx: https://winginx.com/en/htaccess But my web only run home page. This is my config nginx

server {
        server_name www.fdola.com;
        rewrite ^(.*) https://fdola.com$1 permanent;
        }
server {
        listen 80 default_server;
        access_log off;
        error_log off;
        # error_log /home/fdola.com/logs/error.log;
        root /home/fdola.com/public_html;
        include /etc/nginx/conf/ddos2.conf;
        index index.php index.html index.htm;
        server_name fdola.com;

        # nginx configuration. convert from .htaccess root
        location / {
            if ($http_host ~* "^www\.(.*)$"){
                rewrite ^(.*)$ https://%1/$1 redirect;
            }
            rewrite ^/$ /public/ break;
            rewrite ^(.*)$ /public/$1 break;
        }

        # nginx configuration, convert htaccess /public
        charset utf-8;
        location /public {
            if (!-e $request_filename){
                rewrite ^(.*)$ /index.php?_url=/$1 break;
            }
        }

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_connect_timeout 120;
            fastcgi_send_timeout 180;
            fastcgi_read_timeout 180;
            fastcgi_buffer_size 256k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
            fastcgi_intercept_errors on;
            fastcgi_param SCRIPT_FILENAME /home/fdola.com/public_html$fastcgi_script_name;
        }
}

it only work on home page , not work on page rewrite think it's not right on this content, please help me!


location /public {
            if (!-e $request_filename){
                rewrite ^(.*)$ /index.php?_url=/$1 break;
            }
        }
edited Jul '16

structure of my website

edited Jul '16

https://docs.phalcon.io/en/latest/reference/nginx.html

server {
    listen      80;
    root /home/fdola.com/public_html/sub/public; # This path should point the the folder of index.php
    include /etc/nginx/conf/ddos2.conf;
    index index.php index.html index.htm;
    server_name fdola.com;

    location / {
        if ($http_host ~* "^www\.(.*)$"){
            rewrite ^(.*)$ https://%1/$1 redirect;
        }
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        try_files     $uri =404;

        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index /index.php;

        include fastcgi_params;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}

EDIT: .htaccess is for apache only, you can delete those files