📜 ⬆️ ⬇️

One Nginx config for working with a bunch of different sites

If you had to configure Nginx for the needs of a web studio, SEOs or cybersquatters;), then you probably already know about the underscore character as server_name. Nevertheless, several other useful tricks from my example can be gathered.

To create a new site on a server with this configuration, it is enough to create a directory with the site name and upload content to it. And the configuration file remains unique and unchanged.

The config does the following:
1. Cuts off the "www" from the address, so that the server finds the directory with the site despite these letters in the URL.
2. Gives a separate page when requesting a non-existent site.
3. Makes a standard redirect to index.php in the root of the site when requesting a nonexistent path.
4. Forwards the request to php-fpm when calling .php files.
5. Adds the ability to process .htm (l) files as PHP.
')


Directories for sites are created in / var / www / all /.
Simlinks from www to without www are not required.
In the directory / var / www / all / undefined put the site-gag when calling non-existent sites.
If you need to process all html files as PHP in some site, then you should put an empty “.parse_html” file in the root of this site.
Below is the site configuration file. It can be put in / etc / nginx / sites_enabled / default.

The restriction is, perhaps, one thing: rewriting only on index.php in the root. But for 99.9% of the sites that I have to spud, this option is suitable.

server { listen 80 default; #   -   80  server_name _; #  , ,        set $sathost $host; #  sathost    .        #  www if ( $host ~ ^(www\.)?(.+)$ ) { set $sathost $2; } root /var/www/all/$sathost; #     index index.php index.html index.htm; #       access_log off; # access_log /var/log/nginx/all/$sathost_access.log; #  ,  ,  .    error_log /var/log/nginx/all.error.log error; location / { #       if (!-d /var/www/all/$sathost) { #         #      undefined,    /all/undefined set $sathost undefined; rewrite ^ /index.php last; } #  rewrite set $rflag 1; #        ,       . flag      if (-e $request_filename) { #    ,     set $rflag 0; } if (!-f /var/www/all/$sathost/index.php) { #   index.php   ,     ,   set $rflag 0; } if ($rflag = 1) { # rewrite ^ /index.php last; } #  rewrite if (-f $request_filename) { #        expires 1h; break; } } location ~ \.php$ { #      .php  root /var/www/all/$sathost; fastcgi_pass 127.0.0.1:9000; #   php-fpm fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/all/$sathost/$fastcgi_script_name; include fastcgi_params; break; } location ~ \.htm(l?)$ { #      .html  .htm #       «.parse_html»,   HTML  PHP fastcgi_param SCRIPT_FILENAME /var/www/all/$sathost/$fastcgi_script_name; include fastcgi_params; if (!-f $request_filename) { #    .html,         # ,   ,    rewrite rewrite ^ /index.php last; } if (-f /var/www/all/$sathost/.parse_html) { #     #       ,   html   php fastcgi_pass 127.0.0.1:9000; } break; } location ~ /\.ht { #  ,   «.ht»         -     . deny all; } } 

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


All Articles