swoole / library

📚 Swoole Library
https://wiki.swoole.com/#/library
Apache License 2.0
237 stars 58 forks source link

Question: Pretty permalinks example? #30

Closed stefanos82 closed 4 years ago

stefanos82 commented 4 years ago

In Apache we have

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

and in NGINX we have try_files $uri $uri/ /index.php?$args;.

What is the appropriate example with Swoole Proxy to support such behavior?

deminy commented 4 years ago

This script already has everything needed to use Swoole as a proxy to serve a WordPress website:

A few more notes:

Not sure if that answers your question. Feel free to let us know if there is still any questions.

stefanos82 commented 4 years ago

This script already has everything needed to use Swoole as a proxy to serve a WordPress website:

It was the first file I read after the official release of 4.5.0

* Swoole starts a server object listening to port 80 to accept HTTP requests.

* For static content (CSS, images, etc.), they are returned directly.

* Requests to non-static content are processed by PHP-FPM. There is a callback function defined to handle "request" events. All these requests are handled by that function and proxied to PHP-FPM.

* That function forwards the request to PHP-FPM, and sends back whatever returned from PHP-FPM to web browser.

A few more notes:

* The FastCGI client and the proxy is added to use PHP-FPM to process synchronous/asynchronous tasks, and to integrate legacy PHP applications into Swoole (without any changes to the applications themselves, ideally). They provide a new way to have synchronous PHP code to execute asynchronously in Swoole.

* Swoole can be used as a web server directly, just like Apache and Nginx.

That is the ideal way when you don't have a web server installed.

* Swoole doesn't have all HTTP features implemented. In some cases, it's better to put Swoole behind a well known web server like Nginx.

I presume you mean "doesn't have all HTTP features implemented YET!"

Not sure if that answers your question. Feel free to let us know if there is still any questions.

What I want is to remove index.php from permalinks, that's all.

image

deminy commented 4 years ago

Updated the example to make it work with permalinks. Thanks.

stefanos82 commented 4 years ago

Still the same I'm afraid...

image

sy-records commented 4 years ago

You can just use the custom structure. For example, if you need /%postname%/, Write /%postname%/ in the custom structure's input box. No need to fill in /index.php

sy-records commented 4 years ago

By the way, this usage is non-productive, as the production proxy can be used to proxy some HTTP requests from old API interfaces to old FPM services (rather than proxy the whole site).

stefanos82 commented 4 years ago

By the way, this usage is non-productive, as the production proxy can be used to proxy some HTTP requests from old API interfaces to old FPM services (rather than proxy the whole site).

Which usage exactly @sy-records?

Can you please give an example so I can understand your reasoning?

sy-records commented 4 years ago

image Write /%postname%/

stefanos82 commented 4 years ago

Yeah, I have tried that and worked as expected, but I wanted to see whether the NGINX approach could be used here.

Your approach is the same as the Lighttpd's.

sy-records commented 4 years ago

This cannot completely replace nginx.

Nor is it used to proxy the entire site, It makes no sense to proxy the entire site.

stefanos82 commented 4 years ago

That's a bit disappointing :laughing:

deminy commented 4 years ago

Permalinks like http://127.0.0.1/index.php/sample-post/ or http://127.0.0.1/sample-post/ work fine to me.

Swoole is designed to be a high-performance network communication engine in PHP, but not a web server to compete against Nginx, Apache. The WordPress integration script is only to show what we could do with the new Proxy class, using the most popular PHP CMS framework as an example. It doesn't mean we should use Swoole to replace Nginx, Apache, or some other web servers, nor does it mean we should run WordPress like that on production.

The FastCGI client and proxy were added to talk to PHP-FPM processes. Many PHP frameworks and libraries don't work directly under Swoole, but they work well under PHP-FPM (they were created to work under PHP-FPM); it's impossible to ask all the maintainers to refactor their projects to support Swoole. This FastCGI client/proxy opens a door allowing many PHP projects to work with Swoole, without making any code changes on their side.

For example, some PHP libraries and extensions work in blocking mode only (e.g., MongoDB), and they don't yet support coroutine. In this case, we can have them running in PHP-FPM and use the FastCGI client to talk to them asynchronously.

Another use case is to use PHP-FPM to process tasks, especially when the tasks work synchronously only or work under PHP-FPM only.

stefanos82 commented 4 years ago

@deminy thank you for the thorough explanation; it's good to know.

ljfreelancer88 commented 3 years ago

Sorry for my nobe question. Is it possible to use Swoole with PSR15 inside index.php of Wordpress? Something like laravel-s implementation. https://github.com/hhxsv5/laravel-s

Basically the server setup is Nginx+Swoole(0.0.0.0:9501) instead of Nginx(:80)+Swoole proxy(:9501)+PHP FPM(localhost:9000) to eliminate the overhead configuration.

deminy commented 3 years ago

@ljfreelancer88 People have been thinking of running WordPress, Drupal, and some other popular PHP CMS directly in Swoole, but unfortunately, it won't work.

Laravel is implemented in a more modern way (without using global variables, static properties, singletons, etc directly), making it possible to run some Laravel applications in Swoole.