Nginx listens to port 80, I want Nginx not to process it when I request for example.com/tophp , but pass it on to the PHP console application (CLI).

Those. php should also listen, probably, port 80, somehow through sockets (I, in general, in this thread do not know anything).

Maybe some kind of proxying is done on Nginx, a type of web server is raised on php, which listens to port 79, and nginx proxies to it. It is better, of course, that nginx does not handle the example.com/tophp port 80 at all, so that the request goes immediately to php. We'll see how it goes.

Example:

Php script works as an eternal loop, waiting for a request.

... There is a request for http://example.com/test . Nginx processes it, php script does not receive anything.

... There is a request for http://example.com/tophp . Nginx does not process it, but sends it to the PHP cli clipper. And then already in php in the endpoint loop this request is processed as I want.

Tell me, please, how is this generally implemented?

  • in an endless loop is not needed at all. Yes, and no one will allow you to do this, only sockets, and not http - jashka
  • one
    @jashka why in the infinite loop is not necessary? The script should always work - accepted the request, processed it, takes it further. Plus, by passing the request, the cycle executes its actions - Alex

2 answers 2

  1. What is a console PHP application . This is what is performed in the console / terminal, for example:

    php app / console YOU_COMMAND

in essence, this is a bash script that calls a specific script written in php. That is, upon request, you need to run the bash script. To do this, you must install the lua module in NGINX. And in the config there should be something like this:

 location /tophp { content_by_lua 'os.execute("/bin/tophp.sh")'; } 
  1. The same php console script can be run using php. You are accessing the url, and php does something like shell_exec / exec :

shell_exec("php -n -s $File");

shell_exec - function is identical to the operator reverse apostrophe.

That is, the expression can be written as:

 `php -n -s $File` 
  1. Why run php from under the console if you can run it without a console :)

    PHP cli == php

Just create a controller that will perform exactly what in php cli or call the function that in php cli

  1. Work in the eternal cycle. The whole point of php is that it "fades out" after the request is executed. After executing the script php goes to bed. And if all your work is built on an eternal cycle, the correctness of this decision is worth considering. PHP can run background:

    ignore_user_abort (1);
    set_time_limit (0);

You can make a demon in php.

But the php background mode is like a crutch-gag in finding a quick fix, and the php daemon should run from under the console, not the link, because every time you start a daemon, you create a new process that is in perpetual loop.


You wrote:

The script should always work - accepted the request, processed it, takes it further.

Then he does not have to spin forever. According to url, it will start again without any cycle.

    In your case, I can only advise to read this: nginx Rewrite Rules

    Well, in general, you just need to make the necessary rewrite like this:

     rewrite ^/listings/(.*)$ /listing.html?listing=$1 last; 
    • rewrite will not help here. - Alexey