nGinx server_name with regex always being caught (ec2) - amazon-web-services

Have a ec2 server that has different subdomains that i wish to point to different services, trying to use the server_name to catch each but not working, always defaults to first conf (admin). In the example below I want example.com to use default.conf and admin.example.com to use admin.conf.
#nginx.conf
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_comp_level 9;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript applicat$
gzip_min_length 1000;
gzip_disable "MSIE [1-6]\.";
underscores_in_headers on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
index index.html index.htm;
}
#/sites-enabled/default.conf
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
}
#/sites-enabled/admin.conf
server {
listen 80;
server_name ^(www\.)?admin(-dev|-sandbox|)$ *.example.com;
#server_name admin.example.com # This is still caught by all routes
access_log /var/log/nginx/admin.access.log;
location / {
root /usr/share/admin-frontend;
index index.html index.html;
}
}
And I have setup the hosts file
127.0.0.1 localhost
127.0.0.1 admin.localhost
127.0.0.1 admin-dev.localhost
127.0.0.1 admin-sandbox.localhost
Currently if i use the public dns for ec2 (http://ec2-XX-XX-XX-XX.compute-1.amazonaws.com/) the admin config is triggered even though the server doesn't match.

Try this regexp:
server_name ~^(www\.)?admin(-dev|-sandbox|)\.example\.com$;
And I cant find *.example.com domain in your hosts file, maybe you need add them also
127.0.0.1 example.com admin.example.com admin-dev.example.com
etc

Related

Setting Up SubDomain on nginx

i installed nginx on amazon EC2 and the nginx page is showing correctly after correct install, i want to install a subdomain on this web server only, the domain is elsewhere, what should i do. where to start ? i already on my domain's DNS put A record( the IP of this new server).
here is the config file:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}

Nginx " Cannot GET /api " Reverse Proxy

My problem:
I need to do a reverse proxy with Nginx in AWS Linux 2 (Free), and the nginx doesn´t stop throwing me Cannot GET /api ...
Code:
server {
location /api {
proxy_pass http://localhost:3000;
}
}
And nothing the only way this works is with
server {
location /api {
proxy_pass http://127.0.0.1:3000/;
}
}
But don't works the /api/something
Here my Nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
some commands used are:
+sudo iptables -A INPUT -i eth0 -p tcp –-dport 80 -m state –state NEW,ESTABLISHED -j ACCEPT
+sudo amazon-linux-extras install nginx1 -y

Nginx not redirecting traffic from http to https

I have nginx installed on an EC2 server on Amazon Linux 2 and serving my website on HTTP and HTTPS. However, I cannot get the location blocks to be working on HTTP. They work perfectly on HTTPS if I need to serve a file or redirect to a proxy_path, but do not work for HTTP.
I want to redirect all traffic from HTTP to HTTPS.
In addition, even if I remove the first server block, the website is still served through port 80.
server {
listen 80;
server_name *.example.com;
return 301 https://$host$request_uri;
}
Here is my full nginx configuration file located at /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
#include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name *.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name *.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
location /.well-known/acme-challenge/test {
return 200 'test.test';
}
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}
Is there a default configuration file that nginx is using?
Your http block should look like this:
server {
listen 80;
server_name *.example.com;
location / {
return 301 https://$host$request_uri;
}
}
I fixed the issue. The main problem was another process caching the requests and running on the host and listening on ports 80 and 443. Killed the process and everything works fine.

Deploying django application on nginx server rhel - 400 bad request Request Header or cookie too large

I'm currently trying to deploy a Django app on a REHL 7.4 server using Nginx. I've followed these tutorials :
https://simpleisbetterthancomplex.com/tutorial/2017/05/23/how-to-deploy-a-django-application-on-rhel.html
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
The virtualenv and the nginx server seems to be allright. However I'm struggling with two errors:
Either I got a 500 error because of worker_connections parameter value (below are logs):
13494#0: *1021 1024 worker_connections are not enough while connecting to upstream, client: 192.168.1.33, server: 192.168.1.33, request: "GET /Syc/login HTTP/1.0", upstream: "http://192.168.1.33:80/Syc/login", host: "192.168.1.33"
Either I increase worker_connections value to > 4096 and I get a 400 error like in this thread 400 Bad Request - request header or cookie too large
Below are my nginx.conf and app.conf, please let me know if there are configuration mistakes and thanks in advance for any help.
nginx.conf:
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
# set open fd limit to 30000
worker_rlimit_nofile 30000;
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
large_client_header_buffers 4 32k;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
app.conf
upstream app_server {
server unix:/opt/sycoma/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.33; # <- insert here the ip address/domain name
large_client_header_buffers 4 16k;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /opt/sycoma/logs/nginx-access.log;
error_log /opt/sycoma/logs/nginx-error.log;
location /static/ {
alias /opt/sycoma/venv/Sycoma/Syc/static/;
}
location /media/ {
alias /opt/sycoma/venv/Sycoma/media/;
}
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://192.168.1.33;
}
}
Try to remove/comment the line:
proxy_set_header Host $http_host;
or increase large_client_header_buffers.

Setup pretty local development URLs with nginx on Mac OS X

I have a Django server setup that I can access from 127.0.0.1:8000
I have just installed nginx on to Mac OS X Yosemite and can access the 'Welcome to nginx' screen at localhost:80
I want to be able to set up a local development environment on local.ingledow.co.uk but when I visit that URL, it redirects me to my live site: david.ingledow.co.uk.
I did have a wildcard URL forwarder with DNSimple that would redirect like this:
*.ingledow.co.uk > david.ingledow.co.uk
but I have removed that.
I have added this CNAME: local.ingledow.co.uk > 127.0.0.1
This shouldn't affect how I setup my local nginx servers.
So, how do I create an nginx server on Mac that shows my 127.0.0.1:8000 Django project on to a custom domain (local.ingledow.co.uk).
This is my nginx setup:
Default Nginx config at /usr/local/etc/nginx/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# INCLUDES
include /Users/dingledow/etc/nginx/*.conf;
}
ingledow.conf at /Users/dingledow/etc/nginx/ingledow.conf
server {
listen 80;
server_name local.ingledow.co.uk;
location / {
root /Users/dingledow/Documents/Work/Ingledow/git/my_site2/;
index index.html index.htm index.php;
autoindex on;
}
}
I'm not sure if Apache is conflicting with nginx? Or whether I've just setup nginx incorrectly?
Setting a CNAME with your registrar isn't going to help at all, and you should remove that.
The only way to make this work is to edit your local HOSTS file at /etc/hosts and add a pointer to the localhost:
127.0.0.1. local.ingledow.co.uk
Then you'll either need to run the dev server on port 80 instead of 8000, or configure nginx to run your site via gunicorn or uwsgi - the documentation tells to how to do that.