nginx custom config

Questions about HTTP Access/Mods may go here
Forum rules
We've moved! Head over to Synology Community (community.synology.com) to meet up with our team and other Synology enthusiasts!
sinamics
Trainee
Trainee
Posts: 14
Joined: Wed May 07, 2014 1:03 pm

nginx custom config

Unread post by sinamics » Fri Mar 30, 2018 8:03 pm

Hi

I`ve been searching the hole wide web for a solution to my problem without any luck.
I have a SSL website running as VHOST on my RS 814+ with Apache and PHP7 at the moment.

I want to switch over to Nginx, but my site require a custom config block to be added, and i cant understand how to manage that on my NAS.
This is what i want to be added

Code: Select all

    location / { try_files $uri $uri/ /index.php?$query_string; }
    location /api { try_files $uri $uri/ /api.php?$query_string; }
    location /admin { try_files $uri $uri/ /admin.php?$query_string; }

    location ~* ^/(composer\.(json|lock)|config\.php|flarum|storage|vendor) {
        deny all;
        return 404;
    }

    location ~* \.php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTP_PROXY ""; # Fix for https://httpoxy.org/ vulnerability
        fastcgi_index index.php;
    }
    
    location ~* \.html$ {
        expires -1;
    }

    location ~* \.(css|js|gif|jpe?g|png)$ {
        expires 1M;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    gzip on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types application/atom+xml
               application/javascript
               application/json
               application/vnd.ms-fontobject
               application/x-font-ttf
               application/x-web-app-manifest+json
               application/xhtml+xml
               application/xml
               font/opentype
               image/svg+xml
               image/x-icon
               text/css
               #text/html -- text/html is gzipped by default by nginx
               text/plain
               text/xml;
    gzip_buffers 16 8k;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)"; 
Can anyone point me in the right direction. Seems like ever files i have tried get overwritten when i restart nginx.

i have tried to add my code to:

Code: Select all

/etc/nginx/sites-enabled/MyCustomCode.conf   
But i get an error saying locations is not allowed.

Any advice is much appreciated.

kenijo
Trainee
Trainee
Posts: 10
Joined: Thu Aug 30, 2012 8:22 pm

Re: nginx custom config

Unread post by kenijo » Tue Jun 26, 2018 12:55 am

I am stuff too.

I am trying to add a directive to use a new default index.

That works:

Code: Select all

server {
    root            /var/services/web;

    server_name     myserver.com;
    return          301 https://$server_name$request_uri;
}
but that doesn't work:

Code: Select all

server {
    root            /var/services/web;
    index           index.html index.htm index.php /_h5ai/public/index.php;

    server_name     myserver.com;
    return          301 https://$server_name$request_uri;
}

hoylemd
I'm New!
I'm New!
Posts: 4
Joined: Sun Jul 08, 2018 5:19 pm

Re: nginx custom config

Unread post by hoylemd » Sun Jul 08, 2018 6:05 pm

I've been wrestling with this for awhile. The main nginx config is at `/etc/nginx/nginx.conf`, which includes server blocks for DSM, as well as general HTTP and HTTPS blocks, which appear to redirect to DSM, but I'm not quite sure.

The problem is, this config file is somehow the Wolverine of config files. By which I mean that if you change it (with sudo, since it's owned by root), it will just get restored next time you restart the nginx service, so you can't actually change it.

I understand the desire to protect users from their own stupidity, but this is still an offensively heavy-handed solution, Synology. If there's a setting to turn off this 'protection', then please make that more obvious. This is my device. I should be able to decide how it's nginx is configured.

bikerp
Beginner
Beginner
Posts: 29
Joined: Thu Jan 09, 2014 6:56 pm

Re: nginx custom config

Unread post by bikerp » Thu Jul 12, 2018 8:04 pm

Hi,
has anyone solved this problem? I'd like to setup a proxy for backend api. If not using nginx is it possible to do so with Apache?
Thanks for any advice
DS414, 4x WD40EFRX RAID5 (SHR). DSM 6.2

fgtech
I'm New!
I'm New!
Posts: 1
Joined: Tue Jul 17, 2018 7:33 pm

Re: nginx custom config

Unread post by fgtech » Fri Jul 20, 2018 4:39 pm

I ran into the same issues that others encountered here and was able to solve several of them.

The original post by @sinamics mentioned adding code to /etc/nginx/sites-enabled/MyCustomCode.conf but getting an error when trying to create that file. The key insight for me was the realization that the location /etc/nginx/sites-enabled is actually a symlink to /usr/local/etc/nginx/sites-enabled. If you have permission to write files there, then putting a file like MyCustomCode.conf into that expanded path should work. It did for me, anyway.

I love how @hoylemd visualizes the main config file as Wolverine. :twisted: What is happening there is that the fancy web interface for DSM itself uses nginx behind the scenes. It appears to do so by automatically generating various parts of the /etc/nginx/nginx.conf config file in response to options selected in the control panel. So this is less a matter of trying to protect users against themselves than users trying to jump into the system configuration at a place where that was not expected. Try putting your code into a file in the /usr/local/etc/nginx/sites-enabled directory the way @sinamics describes and see if that works for you.

@bikerp: I succeeded in creating an nginx reverse proxy using the above suggestions. Let me know if that works for you. In the end, I figured out that I didn't really want a reverse proxy for my secure web service so I came up with a simpler solution, but perhaps knowing that it does work can help you.

User avatar
spamsupernova
Trainee
Trainee
Posts: 10
Joined: Fri Oct 09, 2015 10:41 am

Re: nginx custom config

Unread post by spamsupernova » Sun Sep 16, 2018 12:07 pm

search for "mustache" files for permanent editing the config files.

Locked

Return to “HTTP/Apache Mods”