How to include location blocks in nginx? - nginx

I'm using nginx as a reverse proxy for 2 web apps.
Those 2 web apps (UI) are sharing location proxies, as the backend services are shared.
How can I combine location blocks and include them in the servers?
host.conf
server {
server_name app1.com
listen 8080;
...
include /opt/bitnami/nginx/conf/vhosts/proxy.conf;
}
server {
server_name app2.com;
listen 8080;
...
include /opt/bitnami/nginx/conf/vhosts/proxy.conf;
}
proxy.conf
location /api/videos {
proxy_pass ...
}
...
But I am getting the following error:
"location" directive is not allowed here in /opt/bitnami/nginx/conf/vhosts/proxy.conf:2

You need to change the file extension, change proxy.conf to proxy.locations.

Related

Apply same content to multiple server blocks in nginx's default.conf file

I got nginxproxy from the docker hub and use it.
I want add a ddos defense to default.conf, which is automatically created in the nginxproxy container.
Since there are many domains, there are several server blocks.
server{
server_name www.domain.com
}
server{
server_name admin.domain.com
}
server{
server_name api.domain.com
}
I want to apply the same information to all server blocks.
limit_req_zone $binary_remote_addr zone=ddos_req:10m rate=5r/s;
limit_req_status 404;
server {
# ...
location /{
limit_req zone=ddos_req;
# ...
}
}
Is it possible to divide and include files or apply the same to all servers with one code?
There is no http block in default.conf, what should I do?

How can a request resolve to a different domain while keeping URI information?

I have 2 domains domain.xyz and domainxyz.com
I have the boilerplate for a dynamic routing service set up on domain.xyz which serves domain.xyz/example-uri.
I would like to make domainxyz.com/example-uri resolve to domain.xyz/example-uri
On the server I am using Nginx to direct requests on port 80 to the React app
instead of using a dns redirect to resolve the .com to the .xyz (which is what I was initially doing), direct both domains to the nginx server. Then configure the nginx server like this:
source/explanation: https://www.nginx.com/blog/creating-nginx-rewrite-rules/
server {
listen 80;
server_name www.domainxyz.com domainxyz.com;
location / {
return 301 $scheme://domain.xyz$request_uri;
proxy_pass http://localhost:3000;
}
}
server {
listen 80;
server_name www.domain.xyz domain.xyz;
location / {
proxy_pass http://localhost:3000;
}
}

nginx Reverse Proxy with 2 servers

So i've came across a cool project and i wanted to recreate it. It is my first time using nginx and also my first time learning things about a reverse proxy. I've currently have a reverse proxy running and it works (I guess). But the Proxy currently only works with other ports. I have 3 servers that are running nginx. I use one of them as my reverse proxy. I can access the other servers with different ports. See here (reverse-proxy.conf):
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.20;
}
}
server {
listen 8080;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.30;
}
}
Are there a way to use the reverse proxy without using different ports? Or is my solution ok? At the end i just need a reverse proxy that is able to communicate with 2 other servers.
So one thing here people use reverse proxy in a different ways
But most generic usecase is redirect using location.
Please find the below example.
server {
listen 80;
root /var/www/html;
server_name localhost;
location /a {
proxy_pass http://192.168.2.20;
}
location /b {
proxy_pass http://192.168.3.20;
}
}
Another is giving weight to each proxy.
Please find the below example
stream {
upstream stream_backend {
server http://192.168.2.20 weight=75;
server http://192.168.3.20 weight=25;
}
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass stream_backend;
}
}
In above 192.168.2.20 will receive 75% of the load and 192.168.3.20 will receive 25% of the load. In case if you want to distribute the equal load to both(or round-robin method) Please remove the weight.
I think you may not understand how Nginx work about proxy.
Nginx can reverse Proxy L7 http or L4 stream
and you set the proxy listen on any port or URL you want and proxy to any server or port or URL you want.
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.20:2323/URL;
}
}
server {
listen 8080;
root /var/www/html;
server_name localhost;
location / {
proxy_pass unix:/tmp/backend.socket;
}
}
Here is a reference for you about the proxy_pass directive.
proxy_pass

Nginx server block ERR_NAME_NOT_RESOLVED

I am running Nginx on an Ubuntu 14.04.5 server. I am trying to set up a new server block but when I navigate to the URL I see this error:
My configuration for this virtual host is below.
The directory that I'd like my subdomain to point to is /var/www/vhosts/ny-video.splashpreviews.com
In /etc/nginx/sites-available is my server block file. The server configuration part of that file is below:
server {
listen *:80;
listen [::]:80;
root /var/www/vhosts/ny-video.splashpreviews.com;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name ny-video.splashpreviews.com;
}
I then enabled the server block and restarted Nginx. Is there something I am missing in the process or something I am doing wrong that is causing this to not work? Any guidance would be appreciated. Thank you.
You need to add splashpreviews.com site to configuration and allow locations of the server. There can be several location sections, limiting access to each subdirectory.
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
include /etc/nginx/mime.types;
....
server {
listen 80;
server_name splashpreviews.com www.splashpreviews.com;
location / {
allow all;
}
....
}
server {
listen 80;
server_name ny-video.sp.com;
location / {
allow all;
}

How to configurate nginx and uwsgi to redirect calls from path to subdomain?

i am running a flask app on uwsgi and nginx. They website serves two functions:
/admin:for backend administration
/api:for rest api
I want all api calls go through the subdomain:api.mysite.com.
It is runing very well if i call api like www.mysite.com/api/v1/cities.
what if i want to call the api this way, api.mysite.com/v1/cities?
How could i manage to do this?
I managed to get it working by setting proxy_pass on nginx conf:
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/app.sock;
}
}
server {
listen 80;
server_name api.mysite.com;
location / {
proxy_pass http://localhost/api/;
}
}
still thinking if it is the best solution and is there any overhead to the server?

Resources