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

Config nginx

Hi there,

I saw some tutorial video in phalcon. When you guys create a new project store. You just need to access localhost/store. I want to config my nginx server to do that too. Example I want to store all phalcon projects in a folder ( D:\Sites\phalcon\ ) Then when I create a new project phalcon ( example store ). I just need to access : phalcon.dev/store/

How can I config in nginx to do that ?

Thanks.

I had similar problem. The problem is with rewrite rule and route but for me it was not a simple one. I try two days to solve that without result. In final I solve it but with different approach by setting a virtual server for each site and thus every site has a clean root. For external access may need to open listen ports (set permissive your firewall rule).

/etc/nginx/sites-enabled/store_a

    server {
        listen   your.local.host.server.ip:80;
        server_name redrent;
        index index.php index.html index.htm;
        set $root_path '/var/www/phalcon.dev/store_a/public';
        root $root_path;
        location / {
            try_files $uri $uri/ /index.php?_url=$uri&$args;
        }
        location ~ \.php$ {
             try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
            root $root_path;
        }
        location ~ /\.ht {
            deny all;
        }
    }

/etc/nginx/sites-enabled/store_b

     server {
         listen   your.local.host.server.ip:81;
         server_name redrent;
         index index.php index.html index.htm;
         set $root_path '/var/www/phalcon.dev/store_b/public';
         root $root_path;
         location / {
             try_files $uri $uri/ /index.php?_url=$uri&$args;
         }
       location ~ \.php$ {
               try_files $uri =404;
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
               fastcgi_pass 127.0.0.1:9000;
               fastcgi_index index.php;
               fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
               include fastcgi_params;
       }
       location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
           root $root_path;
       }
       location ~ /\.ht {
           deny all;
       }
   }

Please note that the listen directive have different ports according with $root_path. Hope to be helpful.



8.1k

You can't use any port. Just use directives listen and server_name. In your example you have 2 sites : store-a and store-b Therefore, in first config you write :


...
listen 80;
server_name store-a;
...

or

...
listen 127.0.0.1:80;
server_name store-a;

and for second site :

...
listen 80; # or listen 127.0.0.1:80

server_name store-b;
...

after that, in /etc/hosts


....
127.0.0.1 store-a
127.0.0.1 store-b
...

@Oleg I don't say that can use ANY port. Your approach is working ONLY for local machine in absence of DNS service. If you want to access your sites from remote position only first site listed in etc/hosts will respond.

From my example:

https://your.server.ip:81 -> return store_a

https://your.server.ip:82 -> return store_b

...and so on but, of course, for a limited number of ports :)

80, 81, 82, 8080, 443(https) or a port number from 9000 through 65535.

To be sure, if are in a Linux system, you can:

less /etc/services

or

sudo netstat -plunt

or

nmap localhost

Regards. drLeo



8.1k

@redpmorg, I can not agree with you unfortunately.

Although I don't work now with Nginx. I work with lighttpd now.

I checked virtual hosts configuration on server today, and I was convinced that all I remember correctly.

This method works for Nginx, well as lighttpd and other server.

https://nginx.org/en/docs/http/request_processing.html

For your remember, /etc/hosts is a primogenitor of DNS. :)