📜 ⬆️ ⬇️

Installing nginx as a front end for Denver

Recently, the installation of nginx as a proxying web server and for the return of statics is becoming quite common practice on the Internet. But besides this, nginx can be used for a heap of other useful things: automatic resize of images, convenient dynamic subdomains , etc.

Previously, we used a copy of the production server to test the nginx settings, but today I finally got tired of going to the server every time to try some little thing, and I decided to set up nginx on the local windows machine where Denver is installed as the server.

Why precisely Denver? Because I think that this is the fastest and simplest way to deploy the environment necessary for development on a windows-machine. Of course, fans of setting up all the little things manually will disagree with me, but it seems to me that tweaking is the lot of production servers, and the local machine has enough default settings.
')
Immediately I warn you that the article is mainly aimed at beginners, and the gurus are unlikely to find anything interesting in it.


Immediately determine that Denver is installed with the default settings, i.e. in particular, the Denver disk is z: \. Also suppose that our local site is called example.local and, accordingly, it was previously available in the browser at example.local and was in the z: \ home \ example.local folder

Apache setup

Go to Z: \ usr \ local \ apache \ conf \ httpd.conf and change the template for the virtual host, respectively, specify port 8080 everywhere - so that the Apache listened to the port other than the default port (80), and then we will hang the 80 nginx, which will process requests from the browser.
## ##    . ## ##       Apache  ,   80, ##       . ## #Listen $&{ip:-127.0.0.1}:$&{port:-8080} #NameVirtualHost $&{ip:-127.0.0.1}:$&{port:-8080} #<VirtualHost $&{ip:-127.0.0.1}:$&{port:-8080}> # DocumentRootMatch "/home/(?!cgi-)(.*)^1/(?!cgi$|cgi-)(.*)" # DocumentRootMatch "/home/(?!cgi-)(.*)/public_html^1" # DocumentRootMatch "/home/(?!cgi-)(.*)/public^1" # DocumentRootMatch "/home/(?!cgi-)(.*)^1/html/(.*)" # DocumentRootMatch "/home/(?!cgi-)(.*)^1/domains/(?!cgi$|cgi-)(.*)" # DocumentRootMatch "/var/www/html/(?!cgi-)~(.*)^1/(?!cgi$|cgi-)(.*)" # DocumentRoot "$&" # ServerName "%&/-www" # ServerAlias "%&/-www" "%&/-www/www" $&{host:-} # # $&{directives:-} # # ScriptAlias /cgi/ "$^1/cgi/" # ScriptAlias /cgi-bin/ "$^1/cgi-bin/" #</VirtualHost> 


After that, the Apache will start listening to port 8080 and our website will be available at exmaple.local : 8080
This is where you can complete the Apache setup.

Nginx configuration


Download from the nginx website the latest version under windows ( nginx / Windows-0.8.53.zip ), unpack it in Z: \ usr \ local \ nginx
Next, configure nginx: file Z: \ usr \ local \ nginx \ conf \ nginx.conf
Here our task is to create a virtual host. In my case, the settings look like this:
  server { listen 127.0.0.1:80; server_name example.local; location / { proxy_pass http://example.local:8080; proxy_redirect http://example.local:8080/ /; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; } location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar)$ { root Z:\home\example.local\www; access_log Z:\home\example.local\example.local.access.log; error_page 404 = @fallback; } location @fallback { proxy_pass http://example.local:8080; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; } } 

Here we have indicated that we are listening to port 80, and that if the request for example.local is received at 127.0.0.1:80, then we proxify this request to example.local: 8080, and, accordingly, we give all the statics just from our root directory.

Denver Setup

But I would like to make nginx run and stop along with Denver so that you do not have to do extra gestures. To do this, we need to add the nginx startup script to the Denver.
Create a file called nginx.pl in the Z: \ denwer \ scripts \ init.d folder
It will contain the code to start and stop the nginx server. I never wrote on a pearl, so I simply took sendmail.pl as a basis and rewrote it under nginx, I got a file with the following contents:
 #!perl -w #    nginx     package Starters::Nginx; BEGIN { unshift @INC, "../lib"; } use Tools; use Installer; use ParseHosts; use VhostTemplate; use StartManager; # Get common pathes. my $basedir = '\\usr\\local\\nginx'; my $startExe = 'nginx.exe'; my $stopExe = 'nginx.exe -s stop'; chdir($basedir); StartManager::action $ARGV[0], start => sub { ### ### START. ### print " NGINX...\n"; if (!-f $startExe) { die "    $startExe.\n"; } else { system("start $startExe"); print " .\n"; } }, stop => sub { ### ### STOP. ### print " NGINX\n"; system("$stopExe"); print " .\n"; }, ; sub checkDaemonIfRunning { } return 1 if caller; 

In order for the information in Russian to be displayed correctly on the command line, the file must be saved in CP-866 encoding.

Now, in order for nginx to start and stop along with Denver, you need to add to the Z: \ denwer \ scripts \ main \ start folder to add a file named 40_nginx (40 means that it will be executed most recently), the contents of the file are just
init.d/nginx
Add the exact same file to the folder Z: \ denwer \ scripts \ main \ stop

Everything, now nginx is turned on and off together with Denver, and our local site, as well as before, is available at example.local , only all static content is now sent using nginx

About the grammatical errors or typographical errors found in the text, please write in a personal or ICQ in order not to litter the comments

Source: https://habr.com/ru/post/108865/


All Articles