How to add basic auth in specific conf if I include location/ in a global Magento conf - nginx-config

I include a global Magento.conf using include /etc/nginx/Magento2.conf
so the location/ block is defined from the global conf above. Now if I want to add basic http auth... where and how do I add .htpasswd location in the site specific vhost config for Nginx.
Just adding the block would throw duplicate location/ block
Many thanks.

Nevermind... it was easy. Never realized you can put auth directive just after the servername directive
Example
server {
listen 95.216.41.220:443 ssl http2;
server_name devdomain.com ;
auth_basic "Restricted Content";
auth_basic_user_file /home/.htpasswd;

Related

Redirect/rewrite nginx location to .sock file without prefix

I have one server that has several APIs running on it. One of them is users-DB The following gets down to gunicorn just fine:
location /usersDB/ {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/projects/UsersDB-api/app.sock;
}
Except when I try to access the usersDB API's /helloWorld route, and look in the logs at gunicorn.err I see:
GET /usersDB/helloWorld
I was hoping to see:
GET /helloWorld
Of course, gunicorn returns 404s and that is what I see in my browser. I've tried rewrite rules:
location /usersDB/ {
rewrite /usersDB/(.*) /$1 last;
include proxy_params;
proxy_pass http://unix:/home/ubuntu/projects/UsersDB-api/app.sock;
}
But the above results in the requests making their way to /var/www/htmlhelloWorld instead of app.sock.
I know that if you use a url for the proxy_pass you just add a trailing /, but I'm not sure what to do in the case of a sock file.
How do I get rid of the /usersDB/ suffix that is now included on all routes in nginx?
Use a separating :. For example:
proxy_pass http://unix:/home/ubuntu/projects/UsersDB-api/app.sock:/;
See this document for details.

Create react app service worker nginx no cache configuration

I am trying to set cache header for service worker through nginx in create react app project, in the configuration, I tried
location /service-worker.js {
add_header Cache-Control "no-cache";
proxy_cache_bypass $http_pragma;
proxy_cache_revalidate on;
expires off;
access_log off;
}
However when I load my page, sw registration fails with the message.
A bad HTTP response code (404) was received when fetching the script.
registerServiceWorker.js:71 Error during service worker registration: TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.
Can someone please suggest a way with nginx using create-react-app?
as per your configuration service-worker.js must be in / root directory defined with root nginx directive.
Please check if the file is present there. If you are using express and express static and have placed the file in public/assets directory, it won't work. if for this file you want to to have different location. you can use alias directive.

Nginx HTTPS subdomain redirect

I have a couple of websites configured with Nginx (nginx/1.10.3 (Ubuntu)) Server Blocks. Browsing to any of the configured domains listed below works as expected:
Those that are configured to use HTTPS get automatic HTTP -> HTTPS redirects (morgrowe.com for example). The issue is when I browse to a subdomain that doesn't exist (for example: doesnotexist.morgrowe.com). If I go to http://doesnotexist.morgrowe.com, I get redirected to the default Nginx page (which is what I want). However, if I go to https://doesnotexist.morgrowe.com, I get redirected to https://api.morgrowe.com.
This wouldn't be so bad, but if I go to https://doesnotexist.carpyslocksmiths.com, I also get redirected to https://api.morgrowe.com.
Where can I find the configuration that makes this happen? Ideally, I'd like the default nginx 404 page to appear like it does for http connections. I imagine I have to configure something in /etc/nginx/sites-available/default, but I don't know where to start. Here's my /etc/nginx/sites-available/default file contents:
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
use a Default “Catch All” Server Block using _ in server_name both for http and https. _ is just an invalid value which will never trigger on a real host.
Please see example From below link
https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/

NGINX Redirect all paths to a specific path

I have a main domain (example.com) and multiple domains (example.net, example1.info, example2.info) pointing to one location in NGINX configuration like so:
server {
server_name example.com *.com *.net *.info;
root /home/example.com/public;
}
And I want to redirect a specific path /login to example.com/login, how do I accomplish it? If I do
server {
server_name example.com *.com *.net *.info;
root /home/example.com/public;
location = /login {
return 301 https://example.com/login;
}
this work for all domains, except example.com/login because it keeps redirecting to itself.
What is the correct way of creating a redirections from a specific path on all sites to my chosen path?
The simplest logic to redirect from one site to another is to isolate the target site with its own server block. Common configuration can be imported into both server blocks by using an include statement.
I would use a default server block rather than a long list of wild cards.
Something like this:
server {
listen 80 default_server;
listen 443 ssl default_server;
include /path/to/common/config;
location = /login {
return 301 https://example.com/login;
}
}
server {
listen 443 ssl;
server_name example.com;
include /path/to/common/config;
}
See this document for details.

How to develop and deploy Catalyst fastcgi apps with nginx and custom URIs?

Probably it's not so relevant, but I'll start with my environment: Linux OS, Perl 5.10, Catalyst 5.80032, nginx 1.0.11.
For the sake of the question, let's suppose I'm using mydomain.com on port 80 as an access point for the app's web page. Also let's use /var/www/mydomain as the physical location of my Catalyst application. In this case the static content is located at /var/www/mydomain/MyApp/root/.
I start the application as a fastcgi server (from MyApp/script):
> ./myapp_fastcgi.pl -l /tmp/myapp.socket -n 2 -p /tmp/myapp.pid -d
I start the nginx server with the following config:
server {
listen 127.0.0.1:80;
server_name mydomain.com;
location / {
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass unix:/tmp/myapp.socket;
}
location /static {
root /var/www/mydomain/MyApp/root;
}
}
and everything seems to work fine when I access the app as http://mydomain.com/ or http://mydomain.com/products as another page (handled by MyApp::Controller::Products).
Now the challenge and the question: how should be nginx configured so it could serve applications' pages with an URI prefix (for example /some/prefix)?
In this case rootpage should be accessed as http://mydomain.com/some/prefix/ and the second one as http://mydomain.com/some/prefix/products.
The second part of the question is: how should be the application code modified in order to have valid URIs for redirects and all the pages? i.e. how $c->uri_for() and similar methods should be (re)written to have the same behavior for prefixed paths?
I have tried the dummy straightforward adjustment
location /some/prefix {
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass unix:/tmp/myapp.socket;
}
but of course it doesn't work this way. So, I'm not able to get responses even to manually typed in URLs before moving to Perl code and play with redirects and URLs generation.
There is in nginx such directive like alias. It works this way:
If you have a url with value of http://my.domain.com/some/prefix/blabla.file and use alias directive with location directive as follows:
location /some/prefix {
alias /var/www/mydomain/MyApp/root;
}
The resulting url will be /var/www/mydomain/MyApp/blabla.file. Why? Because alias directive in location block use it's value (alias value) as temporary root for serving files and request and trim from request part matched in location so when request was /some/prefix/blabla.file here it will be only /blabla.file and when you add this to alias value then you will get what you want (if I understood you well).
According to our discusion belove in comments I can propose two things.
First, in nginx you can add such location block:
location ~ ^/some/prefix(.*)$ {
rewrite ^/some/prefix(.*)$ $1;
}
This directive will delete /some/prefix from /some/prefix/products and leave you only with /products. Next change your location block to looks like following one:
location / {
include fastcgi_params;
# Here you provide for your Catalys real uri which was orignally provided by user
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass unix:/tmp/myapp.socket;
}
So you set param REQUEST_URI for your Catalys to has value of user request without any modifications. In nginx are to variables $request_uri and $uri. Value of this first one never changes and of the second one changes everytime when it's, for example, rewrited by rewrite directive. Moreover this will work because location ~ has higher priority than just location / so request url will be always first trimed to only this what comes after /some/prefix pattern.
Will it be helpfull now?