Recent

Author Topic: Anybody used nginx for Windows?  (Read 5279 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Anybody used nginx for Windows?
« on: October 03, 2021, 11:52:26 am »
Has anybody used nginx on Windows together with fpweb applications?

What are characteristics and benefits of nginx over IIS?

I have written on TWebModule.kind --- wkPooled and wkOneShot previously.  wkPooled mode causes errors when run in FCGI or HTTPServer applications.  So I separated TDataModule which contains database and queries. In FastCGI or HTTPServer, webmodule is created at every request, but datamodule which contains database is created only once.

Even with this, FastCGI causes server errors.  I could remove occurrences of errors by setting FastCGI's max request number per instance to 20 ~ 25 (I wrote about this in previous threads). 

There have been no problem with HTTPserver yet --- I could access several hundreds of times without restarting HTTPServer application without any error. 

I'm running HTTPServer application as Windows service, and this solves the most serious problem (reconnecting the database every request). But this is little bit limited, as it cannot process other requests like requesting simple HTML file (Yes, I could write the program and specify paths, but it's too complicated).   

I read about nginx today (because Lazarus FCGI application template explains to define port for e.g. nginx, etc.). Is nginx flexible as IIS? Does it support fastCGI mode? I'll google for the data, but hope quick replies from those who have used nginx with fpweb.


Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Anybody used nginx for Windows?
« Reply #1 on: October 03, 2021, 12:10:21 pm »
One advantage over IIS is that nginx is cross platform.
Specialize a type, not a var.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Anybody used nginx for Windows?
« Reply #2 on: October 03, 2021, 02:37:38 pm »
I read some materials on the nginx and installed it on my PC. I could see the basic "localhost" page.

Running FastCGI app independently is rather interesting --- hoping that it has the same effect as HTTPServer. But the configuration is too complex for me (not familiar with Linux) now. 

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Anybody used nginx for Windows?
« Reply #3 on: October 03, 2021, 04:32:34 pm »
What are characteristics and benefits of nginx over IIS?
Basically you're not tied to one specific OS so you can develop and deploy on different OSes, just in case you want to deploy on Linux but feeling more comfortable developing on Windows. The configuration can be shared largely, differing only in file paths.
There have been no problem with HTTPserver yet --- I could access several hundreds of times without restarting HTTPServer application without any error. 
Despite was designed mainly for testing during development, I do find the same experience: it's the most stable interface out of all 4.
But this is little bit limited, as it cannot process other requests like requesting simple HTML file (Yes, I could write the program and specify paths, but it's too complicated).
This can be delegated to nginx, let your HTTP application serves data only, not static files. Real world deployment will consider CDN instead, so you have even less traffic burden.
Is nginx flexible as IIS?
No, it's A HELL LOT MORE flexible ;)
Does it support fastCGI mode?
Both FastCGI and reverse proxy are supported built-in, CGI (and obviously Apache module) do(es)n't. I use reverse proxy, though, not FastCGI due to stability reason above, but your mileage may vary.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Anybody used nginx for Windows?
« Reply #4 on: October 03, 2021, 06:31:54 pm »
@ Leledummbo,

Thank you for your kind replies. I tried some, and now I can run FCGI through nginx.

First, I run my fcgi program as stand-alone (I didn't know this works. What's difference from HTTPserver?)

Following is the whole codes I wrote.  The default ACTION is Hello, which says something like 'Hello,World'.

I added another action "Morning", which says "Good Morning".


Code: Pascal  [Select][+][-]
  1. program fcgi_1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   fpFCGI, uwm1;
  7.  
  8. begin
  9.   Application.Title:='fcgiproject1';
  10.   { Uncomment the port setting here if you want to run the
  11.     FastCGI application stand-alone (e.g. for NGINX) }
  12.   Application.Port:=2015; // For example
  13.   Application.LegacyRouting := true;
  14.   Application.Initialize;
  15.   Application.Run;
  16. end.
  17.  
  18.  
  19. unit uwm1;
  20.  
  21. {$mode objfpc}{$H+}
  22.  
  23. interface
  24.  
  25. uses
  26.   SysUtils, Classes, httpdefs, fpHTTP, fpWeb;
  27.  
  28. type
  29.  
  30.   { Twm1 }
  31.  
  32.   Twm1 = class(TFPWebModule)
  33.      procedure MorningRequest(Sender: TObject; ARequest: TRequest;
  34.         AResponse: TResponse; var Handled: Boolean);
  35.   private
  36.  
  37.   public
  38.  
  39.   end;
  40.  
  41. var
  42.   wm1: Twm1;
  43.  
  44. implementation
  45.  
  46. {$R *.lfm}
  47.  
  48. { Twm1 }
  49.  
  50. procedure Twm1.MorningRequest(Sender: TObject; ARequest: TRequest;
  51.    AResponse: TResponse; var Handled: Boolean);
  52. begin
  53.    AResponse.Content:= 'Good Morning';
  54.    Handled:= true;
  55. end;
  56.  
  57. initialization
  58.     RegisterHTTPModule('wm', Twm1);
  59. end.
  60.  

By pressing F9, this is running listening to port 2015.

So, I defined in the nginx.conf file as follows.

Code: Text  [Select][+][-]
  1.         location /aqrun/  {
  2.             fastcgi_pass   127.0.0.1:2015;
  3.         #    fastcgi_index  index.php;
  4.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  5.             include        fastcgi_params;
  6.         }

And... http://localhost/aqrun  displays "Hello World", but I cannot run localhost/aqrun/morning.  This displays "Hello, World" as well. How can I define nginx to process pathinfo?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Anybody used nginx for Windows?
« Reply #5 on: October 04, 2021, 09:12:41 am »
I'm running HTTPServer application as Windows service, and this solves the most serious problem (reconnecting the database every request). But this is little bit limited, as it cannot process other requests like requesting simple HTML file (Yes, I could write the program and specify paths, but it's too complicated).

So RegisterFileLocation from fpWebFile isn't enough for you?

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Anybody used nginx for Windows?
« Reply #6 on: October 04, 2021, 10:04:36 am »
Quote
So RegisterFileLocation from fpWebFile isn't enough for you?

I think this did not work correctly on Windows. Does it?

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Anybody used nginx for Windows?
« Reply #7 on: October 04, 2021, 10:54:26 am »
hello,
BrookFreePascal (in OPM) works on windows with nginx  using fastcgi.
here is  how to route with brook  :
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   BrookAction;
  9.  
  10. type
  11.    { TDoDefault }
  12.  
  13.   TDoDefault = class(TBrookAction)
  14.   public
  15.     procedure Get; override;
  16.   end;
  17.  
  18.   { TDoHello }
  19.  
  20.   TDoHello = class(TBrookAction)
  21.   public
  22.     procedure Get; override;
  23.   end;
  24.   { TDoMorning }
  25.  
  26.   TDoMorning = class(TBrookAction)
  27.   public
  28.     procedure Get; override;
  29.   end;
  30.  
  31. implementation
  32. { TDoDefault }
  33.  
  34. procedure TDoDefault.Get;
  35. begin
  36.   Write('<html><body><h1>FastCGI!</h1></body></html>');
  37. end;
  38.  
  39. { TDoHello }
  40.  
  41. procedure TDoHello.Get;
  42. begin
  43.   Write('<html><body><h1>Hello World!</h1></body></html>');
  44. end;
  45. { TDoMorning }
  46.  
  47. procedure TDoMorning.Get;
  48. begin
  49.   Write('<html><body><h1>Good Morning!</h1></body></html>');
  50. end;
  51.  
  52. initialization
  53.   TDoDefault.Register('/');
  54.   TDoHello.Register('/dohello');
  55.   TDoMorning.Register('/domorning');
  56.  
  57. end.
fascgi config of my nginx server :
Quote
location /aqrun/ {
        #     root           html;
             fastcgi_pass   127.0.0.1:9000;
        fastcgi_split_path_info ^(/aqrun/)(.*)$;
        fastcgi_param  PATH_INFO          $fastcgi_path_info;
        #    fastcgi_index  index.php;
        #     fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
             include        fastcgi_params;
        }
i use the port 9000 because port 2015 is already used in my system.
Project in attachment.
Friendly, J.P


Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Anybody used nginx for Windows?
« Reply #8 on: October 04, 2021, 12:08:27 pm »
Hello Jurassic Pork,

Really really thank you very much.  I tested your configuration only with one change which is following line, and it works very well with my previous fpweb application.

       include      fastcgi.conf;  # fastcgi_params;

I had difficulty with handling   fastcgi_split_path_info ^(/aqrun/)(.*)$;  ,  on which you gave a great solution.  Many thanks again.


Just one thing to note : 

I can pass query fields, like     localhost/aqrun/morning?good=evening , but passing action as query parameter does not work. i.e,

     localhost/aqrun?action=morning  does not work.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Anybody used nginx for Windows?
« Reply #9 on: October 04, 2021, 01:27:17 pm »
I successfully finished quick testing of NGINX with FastCGI --- there were no errors which occurred with IIS.  (Haven't tried wkPooled mode. I don't think this will work as wkPooled mode had problems even in HTTPServer).   I don't know whether Apache server has the same problem as IIS.

I suspect NGINX's asynchronous event‑driven approach might be related with this issue. Hope that anybody who has deep knowledge can explain.

And I appreciate advices from Leledumbo, Jurassic Pork, and PascalDragon.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Anybody used nginx for Windows?
« Reply #10 on: October 04, 2021, 01:36:30 pm »
Quote
So RegisterFileLocation from fpWebFile isn't enough for you?

I think this did not work correctly on Windows. Does it?

With FPC 3.2.2 it should work correctly as long as you either use a relative (.\whatever) or an absolute (D:\whatever) path. What won't work are drive relative (D:whatever) or drive absolute (\whatever) paths.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Anybody used nginx for Windows?
« Reply #11 on: October 04, 2021, 05:24:53 pm »
Sarah, in that case canonical paths *should* work. I will write a little test.
« Last Edit: October 04, 2021, 05:26:44 pm by Thaddy »
Specialize a type, not a var.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Anybody used nginx for Windows?
« Reply #12 on: October 04, 2021, 07:53:46 pm »
First, I run my fcgi program as stand-alone (I didn't know this works. What's difference from HTTPserver?)
The underlying protocol differs. HTTPServer really does what it says: it hosts a HTTP server. You can directly query through curl or any other HTTP clients. FastCGI cannot, you will need a webserver capable of communicating with a FastCGI app as the middle layer, you will still send your HTTP request to the webserver, not the FastCGI app.

nicanor

  • New Member
  • *
  • Posts: 17
Re: Anybody used nginx for Windows?
« Reply #13 on: October 04, 2021, 10:11:46 pm »
I'm stuck ...
I tried the @egsuh example here in my computer whit ubuntu...
I just added

Code: [Select]
        location /test/ {
                # include fastcgi.conf;
                include fastcgi_params;
                fastcgi_pass 127.0.0.1:9100;
                # fastcgi_split_path_info ^(/teste/)(.*)$;
                # gzip off;
        }

in /ect/nginx/sites-available/default and restarted nginx...

before restart I sudo nginx -t  and returned everting is ok  .

When I built my test app with (commented Application.port):

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   fpFCGI, unit_principal;
  7.  
  8. begin
  9.   Application.Title:='fcgiproject1';
  10.   { Uncomment the port setting here if you want to run the
  11.     FastCGI application stand-alone (e.g. for NGINX) }
  12.   //Application.Port := 9100; // For example
  13.   Application.Initialize;
  14.   Application.Run;
  15. end.

and try to access http://localhost/test/test, I got a "502 Bad Gateway" in the browser with a   

2021/10/04 16:37:47 [error] 1168#1168: *21 connect() failed (111: Unknown error) while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /test/test HTTP/1.1", upstream: "fastcgi://127.0.0.1:9100", host: "localhost"    in the   tail -f /var/log/nginx/error.log

if I give a sudo spawn-fcgi -a127.0.0.1 -p9100 /var/www/http/test/test, works ok, create a process, but http://localhost/test/test returns:

fcgiproject1: ERROR

The application encountered the following error:
    Error: Not found
    Stack trace:
    $00000000004B216F
    $00000000004B17D2

If I uncomment Application.Port := 9100 and rebuild ...

sudo spawn-fcgi -a127.0.0.1 -p9100 /var/www/http/test/test, it doesn't work and http://localhost/test/test always returns: "502 Bad Gateway" doesn't matter combination of comments/uncomments in

Code: [Select]
location /test/ {
                # include fastcgi.conf;
                include fastcgi_params;
                fastcgi_pass 127.0.0.1:9100;
                # fastcgi_split_path_info ^(/teste/)(.*)$;
                # gzip off;
        }

[Edited to add code tags - please read How to use the Forum.]
« Last Edit: October 05, 2021, 01:03:39 am by trev »

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Anybody used nginx for Windows?
« Reply #14 on: October 05, 2021, 01:33:39 am »
First try to change as follows in nginx.conf file (not sure whether this has effect or not).  But the first one did not work in my case.

   include fastcgi_params;   -->  include      fastcgi.conf;

If you can see nginx's welcome screen on browser at "localhost", then with program test,

  //Application.Port := 9100;   //    ->  UNCOMMENT this line,

And you'd better to add Application.Legacyrouting := true; in the program file.

I assume that unit_principal is TFPWebModule.  Please check whether it has default action, and its DefActionWhenUnknown property is set to true. 

Press F9.  In Windows, there appear a command window. If it does not display a small command window, press Shift-Ctrl-F9. The test application must be running.

Then

   http://localhost/test     must work.  This should display the default action.

In order to make http://localhost/test/test , you need following definitions in nginx.conf

       fastcgi_split_path_info ^(/aqrun/)(.*)$;
       fastcgi_param  PATH_INFO          $fastcgi_path_info;


Without these lines, the second test is not processed. It will always do default action.

Program name "test" has nothing to do with "test" in the URL. You must have an Action named test defined in the web module.


 

TinyPortal © 2005-2018