Nginx how to add extra server configuration to without modifying nginx.conf - nginx

I want to add this extra config to my nginx.conf:
server {
listen 0.0.0.0:8081;
rewrite ^ https://$host$request_uri? redirect;
}
But as my app is deployed in a hosting service I don't want to modify the already present nginx.conf. It can be problematic.
Is there any way I can add this extra configuration without modifying nginx.conf?

There is no way you can add extra server configuration without modifying nginx.conf first. But good news is that you will have to modify nginx.conf only for once.
Just add this line in your nginx.conf
include /etc/nginx/config.d/*.conf;
You can name directory and path as per your choice.
create directory and save your extra configuration in that as extra.conf with .conf extension. Any files you save with .confextension in this directory /etc/nginx/config.d will be automatically added to your nginx.conf.
You can even save multiple configurations like extra1.conf and extra2.conf
for different uses and you can delete one without affecting other.

There is one way, but you need to insert some changes to nginx.conf
you can create a template file extra_config that contains
server {
listen 0.0.0.0:8081;
rewrite ^ https://$host$request_uri? redirect;
}
and in nginx.conf add this string
{% include '%path_to_template_file%/extra_config'}

Related

Redirecting to other url when specific url is called from the browser through nginx

I have hosted my JavaScript application in aws ec2 server. I'm using centos 7 and placed my JavaScript static files in /usr/share/nginx/html directory. I'm using nginx as reverse proxy.I have one yaml file inside this JavaScript files that is accessible through the browser. The yaml file contains all the sensitive information but that file is necessary to load my application. Is there any way to disable direct access to that file in browser. Or is there any way to disable that information disclosure in browser through nginx.Thanks in advance for the help.
I have tried
location /properties.yaml {
rewrite ^/$ /login.html permanent;
}
But this is not working. Then i tried to read that file from other location like
location /properties.yaml{
try_files $uri /var/www/html/properties.yaml;
}
Could you please guide.

Nginx keeps redirecting and adding tailing slashes

I am trying to redirect an app to some static html files.
I am using NGINX as a reverse proxy, and alias to use a specific path.
So I am using a simple Redirect within a react container:
redirectTo="/documentation/index-docs.html"
This is the Nginx config part used for that:
location /documentation {
alias /workspace/documentation;
rewrite ^(.+)/+$ $1 permanent;
The current behavior is that it keeps looping and adding tailing slashes.
http://localhost:3001/documentation/index-docs.html/////////////
Did anyone encounter the same issue before ?
Thanks.

Nginx serving files from non-domain url?

Is it possible for me to have an nginx server's domain to contain slashes? For example, for the server's root url location to be https://example.com/apps/app1?
I have a server whose files need to be served from /opt/production/app1/public. My current nginx.conf configuration, which doesn't work, contains:
location /apps/app1 {
root /opt/production/app1/public;
}
But obviously, this doesn't work because my files aren't at /opt/production/app1/public/apps/app1. I would like for nginx to consider https://example.com/app/apps1 to be my domain, so that my nginx.conf can access content as so:
location / {
root /opt/production/app1/public;
}
Is this at all possible? If not (which I suspect is the case), is there a way to work around this w/o changing the url schema?
If I understand your question correctly, you can try an alias directive for /apps/app1/ location:
location /apps/app1/ {
alias /opt/production/app1/public/;
}

DSM6 : Configure Nginx for Wordpress permalinks (avoid 404)

With the Synology DSM6 update, we have now to use Nginx instead of Apache. By default Nginx configuration don't allow wordpress permalinks (generate 404).
I read the idea was to transform the /uri in /?p=$uri and put this configuration in the "location" section of the server nginx config.
Where to put this configuration in DSM6 exactly ?
Have you tried the user config? Just copy your working:
/etc/nginx/app.d/server.webstation-vhost.conf
to:
/usr/local/etc/nginx/sites-enabled/httpd-vhost.conf-user
and rename the server.webstation-vhost.conf to server.webstation-vhost.conf.old or something and restart nginx (nginx -s reload)
Or better yet, remove your virtual host(s) from webstation. Only thing is you need to manually update your SSL certs when they expire instead of using the web interface.
Actually, you can add custom directives easily, without modifying the DSM behavior.
Take a look at the content of /usr/local/etc/nginx/sites-enabled/httpd-vhost.conf-user, to see where the custom configuration has to be stored:
server {
[...]
server_name NAME
[...]
include /usr/local/etc/nginx/conf.d/778943ad-0dc4-40ae-bb7f-7b2285e3203b/user.conf*;
}
Then, you just have to create the file /usr/local/etc/nginx/conf.d/778943ad-0dc4-40ae-bb7f-7b2285e3203b/user.conf.wordpress-permalink with the following content:
location /{
try_files $uri $uri/ /index.php?$args;
}
and restart nginx:
synoservicecfg --restart nginx
It will not break the future DSM update (since it is a supported customization)

Correct NGINX rewrite rule for static files

I want to create a NGINX rewrite rule for static CSS and JavaScript files.
E.G Browser request: www.website.com/staticfiles/generatedhashhere/css/file.css
Nginx shoold look to: /staticfiles/css/file.css
I want to exclude one subdirectory path.
Is it possible to write rule with NGINX try_files?
Thanks for your help!
I think you are making it more complex than it needs to be. Instead of using a try_files statement for this, it would be easier to simply set up location blocks for CSS and javascript files with the directory configured for each one.
Example:
location ~* \.(css|js)$ {
root /staticfiles/css/;
}
If you want it to be a rewrite, then replace root statement with
rewrite ^/staticfiles/generatedhashhere/css/(.*)$ /staticfiles/generatedhashhere/css/$;

Resources