Nginx Directive Snippets : Adding in extra curly bracket

Discussion in 'Server Operation' started by hendr1x, Aug 27, 2023.

Tags:
  1. hendr1x

    hendr1x New Member

    Hello everyone,
    Having a hard time moving all of my sites over from directadmin. I have some nginx configuration code that I've used for years... my current iteration trying to get working in ISPConfig is below

    Code:
    location ~* ^\/(template|image).+\.(css|js|jpeg|gif|png|jpg|woff|woff2|ttf){
        include /etc/nginx/mime.types;
        access_log      off;
        add_header      Cache-Control   "public";
        add_header      Pragma          "public";
        expires         30d;
        log_not_found   off;
        tcp_nodelay     off;
        try_files       $uri =404;
    }
    
    location / {
        include /etc/nginx/mime.types;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/lib/php8.2-fpm/web4.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_read_timeout 108000;
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location @php {
        include /etc/nginx/mime.types;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/lib/php8.2-fpm/web4.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_read_timeout 108000;
        try_files $uri $uri/ /index.php?$query_string;
    }
    Everytime I add this, it automatically adds a bracket (see below)...which produces an error and it saves it as .vhost.err which obviously don't utilize it. Please help. I have a customer site down, it was suppose to be 5 minutes and I've already spent a few hours on this. I want all files in web (php, images, css etc) to be served directly. Everything else should get sent to web/index.php for processing. I added the @php because I THINK I need to override the existing declarative in your default configs. Thanks for any help you can provide.

    Code:
            location ~* ^\/(template|image).+\.(css|js|jpeg|gif|png|jpg|woff|woff2|ttf){ {
                    include /etc/nginx/mime.types;
                    access_log      off;
                    add_header      Cache-Control   "public";
                    add_header      Pragma          "public";
                    expires         30d;
                    log_not_found   off;
                    tcp_nodelay     off;
                    try_files       $uri =404;
            }
    
     
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    Nginx does not allow you to duplicate locations or override locations by duplicating them, that's why you must get a .err file if you do so. ISPConfig has a special syntax to allow you to merge and delete config sections to overcome this limitation in Nginx config file parser. You might want to get a copy of the ISPConfig manual, it is described on page 150 on how to use ##delete## and ##merge## to delete or merge sections in a Nginx config file.
     
    ahrasis likes this.
  3. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    Other than the ISPConfig manual, nginx directive had been discussed numerous times in this forum. To find them try googling:
    site:forum.howtoforge.com nginx directive root ##delete## ##merge##
     
    Last edited: Aug 28, 2023
  4. hendr1x

    hendr1x New Member

    @till + @ahrasis : Thank you both for the fast response. Especially considering it is Sunday! I implemented the ##delete## syntax and it worked perfectly.

    Unfortunately the double bracket is still getting generated. I confirmed that it is not present in my nginx directives textarea and I tried a few different times...deleting the .err file as well and letting it get generated again. Always the exact same issue...as above...an extra curly bracket being inserted.
     
  5. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    Are you using any custom conf?
     
  6. till

    till Super Moderator Staff Member ISPConfig Developer

    I have not seen a curly bracket being added on any of my Nginx systems. Try changing:

    Code:
    location ~* ^\/(template|image).+\.(css|js|jpeg|gif|png|jpg|woff|woff2|ttf){
    to

    Code:
    location ~* ^\/(template|image).+\.(css|js|jpeg|gif|png|jpg|woff|woff2|ttf) {
    by adding a whitespace between ) and {
     
  7. hendr1x

    hendr1x New Member

    Alright. @till adding the whitespace fixed the issue. @ahrasis no other conf at all. No other sites using the nginx directive yet.

    My site refused to redirect to index.php because of this snippet :

    Code:
          location ~ \.php$ {
                try_files /adef160bcff63d39e60932179a37b192.htm @php;
            }
    
    I removed it and the site is running properly but I don't know what it was for. Could someone please explain what it does? I purchased a manual and tried to find an explanation but could not. However, the paypal button on the payment page does not work with my popup blocker (chrome = adblock + ublock). I figured I would let you know.

    Thanks a million for help both of you. I really appreciate it.
     
  8. hendr1x

    hendr1x New Member

    Actually...if I could also add one more question. Was I notified somewhere that the nginx error was occurring? As a new user, my experience was that everything was running fine. It took a while of me digging to find what was happening above. Perhaps there is an error log I am not aware of?
     
  9. till

    till Super Moderator Staff Member ISPConfig Developer

    You can find all kinds of logs, incl. the system log from ISPConfig in the ISPConfig monitor section, where ISPConfig notifies you about problems with applying new config. You can also subscribe to them by email (see settings under System > interface > main config for admin address and System > server config for log levels and email log level) and you can also find website-specific logs in the log folder of the website. And besides there options, there is also the capability to do extensive debug logging: https://www.faqforge.com/linux/debugging-ispconfig-3-server-actions-in-case-of-a-failure/

    This snippet is there to enforce handling of .php files with the PHP handler. It is fine and does not need to be removed, your config basically overrides all settings from within ISPConfig incl. PHP version selection etc., that's why your config causes issues.

    E.g. for sites that have a global index.php script like WordPress does, all you add in Nginx directives field is this:

    Code:
     location / {
                    try_files $uri $uri/ /index.php?$args;
           }
    and if you also want to disable logging for some file types in a WordPress, all you use is this:

    Code:
    location / {
                    try_files $uri $uri/ /index.php?$args;
           }
    
           # Add trailing slash to */wp-admin requests.
           rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    
           location ~*  \.(jpg|jpeg|png|gif|css|js|ico)$ {
                    expires max;
                    log_not_found off;
           }
     
    ahrasis likes this.
  10. hendr1x

    hendr1x New Member

    I just confirmed what I was experiencing.
    Code:
    location ~ \.php$ {
    Is running and getting my requests. I tried all different combinations and I could not stop this from occurring. I had to delete it.
    Did not work. It produced 404 errors. I also seem to have to delete @php because I want to send all 404 traffic to index.php not just *.php.

    So, if you have the patience I am 100% open to hearing how I can set things up better. However, afaik my current configuration works (at bottom) below. The only thing I do not feel comfortable about is having to deleting

    Code:
    location ~ \.php$ {
               try_files /adef160bcff63d39e60932179a37b192.htm @php;
           }
    
    I don't understand what it is doing and I want to make sure I don't break anything. Thanks for your help

    ### Current NGINX directive

    Code:
    location @php { ##delete## }
    location ~ \.php$ { ##delete## }
    
    location ~* ^\/(template|image|css).+\.(css|js|jpeg|gif|png|jpg|woff|woff2|ttf) {
        include /etc/nginx/mime.types;
        access_log      off;
        add_header      Cache-Control   "public";
        add_header      Pragma          "public";
        expires         30d;
        log_not_found   off;
        tcp_nodelay     off;
        try_files       $uri =404;
    }
    
    location / {
        include /etc/nginx/mime.types;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/lib/php8.2-fpm/web4.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_read_timeout 108000;
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    
     
  11. till

    till Super Moderator Staff Member ISPConfig Developer

    This is working fine. I use it on many WordPress sites. But it might fail due to the other config you added which disabled PHP selection etc. So if you are using WordPress, then empty the Nginx directives field and add this (copied from a live WordPress site on an ISPConfig system):

    Code:
    location / {
                    try_files $uri $uri/ /index.php?$args;
           }
    
           # Add trailing slash to */wp-admin requests.
           rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    
           location ~*  \.(jpg|jpeg|png|gif|css|js|ico)$ {
                    expires max;
                    log_not_found off;
           }
    Nothing else is needed for WP.
     
  12. hendr1x

    hendr1x New Member

    Thank you for your patience. I would like to get this right.

    Just to start fresh/keep things simple. I copy and pasted your code above. It produces an 404 when I go to /admin/index.php. I confirmed this file does not exists in the web directory. I outlined what I did to solve things above. Would you be willing to give me what you think the next step should be to figure out what is going on?
     
  13. till

    till Super Moderator Staff Member ISPConfig Developer

    Ok, so you have a CMS where .php file URL's shall not get parsed by PHP. That's quite uncommon as one normally has virtual URL's like /somefolder/article/ (e.g. as you see here on Howtoforge or in any WordPress site or even here in the XenForo forum), that get handled by a central PHP script, but if a URL points to a PHP file with .php file ending directly and not a virtual URL, then this gets run by PHP. That's the way most CMSs incl. WordPress are doing it. In your special case where .php files shall not get parsed by PHP, you have to remove the handler that assigns the .php file ending to the PHP parser.
     
    ahrasis likes this.
  14. hendr1x

    hendr1x New Member

    @till : My CMS is just like Wordpress. URLs get parsed by PHP. It has virtual URLs. The only difference is I want other types of files (for example pdf and xlsx) to also get parsed by PHP. The nginx configuration I posted in my first post works perfectly. It explains how the CMS works.

    Thanks again for your patience. I know this is taking a long time to figure out.
     
  15. till

    till Super Moderator Staff Member ISPConfig Developer

    From your post:

    If your system would work like WordPress, then the file /admin/index.php would exist in the directory and the Nginx config for WordPress would work in this case. As I described in #13, you are using URL's with .php ending as virtual URL's, which must fail when you have a handler for .php files in Nginx config. In Wordpress, any URL with .php file ending exists as a file, while virtual URL's that are parsed by the global URL mapper have no .php file ending.
     
  16. hendr1x

    hendr1x New Member

    Oh, I didn't realize how Wordpress worked. Thank you for explaining it.

    Could you please confirm that this is the best configuration for my system? What I want is for all files to get served directly. If it is not found then it sends it to /index.php


    Code:
    location @php { ##delete## }
    location ~ \.php$ { ##delete## }
    
    location ~* ^\/(template|image|css).+\.(css|js|jpeg|gif|png|jpg|woff|woff2|ttf) {
        include /etc/nginx/mime.types;
        access_log      off;
        add_header      Cache-Control   "public";
        add_header      Pragma          "public";
        expires         30d;
        log_not_found   off;
        tcp_nodelay     off;
        try_files       $uri =404;
    }
    
    location / {
        include /etc/nginx/mime.types;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/lib/php8.2-fpm/web4.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_read_timeout 108000;
        try_files $uri $uri/ /index.php?$query_string;
    }
     
  17. till

    till Super Moderator Staff Member ISPConfig Developer

    You should replace the line:

    Code:
    fastcgi_pass unix:/var/lib/php8.2-fpm/web4.sock;
    with the placeholder:

    Code:
    {FASTCGIPASS}
    so that you can manage the PHP version trough ISPConfig.
     
  18. hendr1x

    hendr1x New Member

    Thank you so much. I'm super impressed by this software + the community. Thanks @till + @ahrasis ! I'm sure I'll see you around.
     
    Th0m, ahrasis and till like this.

Share This Page