Configure nginx vhosts by path - nginx

I want to serve my projects by first item in the path, for example http://example.com/projectname should serve a project in /usr/share/nginx/html/projectname.
This is what my configurations look like:
server {
listen 80;
server_name example.com www.example.com;
rewrite ^/(.*) https://example.com/$1 permanent;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate "/etc/ssl/XX.pem";
ssl_certificate_key "/etc/ssl/XX.key";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
server_name example.com/$1 www.example.com/$1;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location /projectname {
root /usr/share/nginx/html/projectname ;
index index.html;
try_files $uri $uri/ /index.html?$args;
}
}
Observation:
When i visit the configured domain it routes to nginx defualt page instead of displaying the expected project.

3 Changes:
Instead of rewrite, do return 301
In second server block, don't have /$1 at the end of server_names
remove index index.html from location /projectname block
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com/$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate "/etc/ssl/XX.pem";
ssl_certificate_key "/etc/ssl/XX.key";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
server_name example.com www.example.com;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location /projectname {
root /usr/share/nginx/html/projectname ;
try_files $uri $uri/ /index.html?$args;
}
}
Try this and it should work.

1: Open sudo vi /etc/hosts file in you Linux machine
2: 127.0.0.1 example.com www.example.com
3: Save and exit.

Related

Nginx is not serving static files using alias and root?

I have been trying to serve some static files using NGINX server but despite the configurations I haven't been able to get it running. I have tried using alias, root and even regex match - what could be missing?
/home/user/coka/staticfiles/ contains all the files i want to serve but whenever i visit http://127.0.0.1/staticfiles/file.css or http://example.com/staticfiles/file.css it is not showing up.
My configuration is shown below:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
location /staticfiles/ {
root /home/user/coka/staticfiles/;
access_log /home/user/coka/logs/nginx-static-access.log;
error_log /home/user/coka/logs/nginx-static-error.log;
}
}
server {
listen 80;
listen [::]:80;
server_name example.com;
return 301 https://example.com$request_uri;
}
I have been getting error 404.
Can you try the following?
ssl_session_cache shared:SSL:4m; # measured in megabytes, not minutes
ssl_buffer_size 4k; # reduced from the default 16k to minimize TTFB
ssl_session_timeout 60m;
ssl_session_tickets off;
ssl_dhparam /etc/ssl/nginx/dhparam.pem; # create with "openssl dhparam -out dhparam.pem 4096"
ssl_ecdh_curve X25519:sect571r1:secp521r1:secp384r1;
ssl_prefer_server_ciphers off;
ssl_protocols TLSv1.3 TLSv1.2;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_certificate /etc/ssl/chain.pem;
ssl_certificate_key /etc/ssl/key.pem;
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
root /home/user/coka;
access_log /home/user/coka/logs/nginx-static-access.log;
error_log /home/user/coka/logs/nginx-static-error.log;
}
server {
listen 80;
listen [::]:80;
server_name example.com;
return 301 https://example.com$request_uri;
}

NGINX Too Many Redirects

I'm doing a proxy server with NGINX to redirect all traffic from port 80 to port 443 and then all traffic from port 443 to an app in one of my servers. I managed to make it work but only redirecting to my main IP 192.168.1.201:8006. When I try to point to my app (192.168.1.201:8006/customerSite/)the page gives me the error TOO MANY REDIRECTS.
Here is my .conf:
server {
listen 80;
listen [::]:80;
return 301 https://$host$request_uri;
}
This is my ssl.conf:
server {
listen 443;
listen [::]:443;
server_name mydomain.com;
ssl on;
ssl_certificate /.../certificate.crt;
ssl_certificate_key /.../clientsmydomain.key;
large_client_header_buffers 4 10k;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /.../certificate.crt;
location / {
proxy_set_header Host $host;
proxy_pass http://192.168.1.201:8060;
}
location /weights {
root /var/www/virtual/server;
try_files $URI $uri/ = 404;
}
}
Thank you all for your time.

nginx rewtire doesn't work

I'm trying to rewrite from short name to FQDN.
My nginx version is
nginx version: nginx/1.13.4
server {
listen 80;
server_name foo foo.bar.com;
rewrite_log on;
rewrite ^ https://foo.bar.com/ permanent;
include includes/web-site;
include includes/files-site;
}
server {
listen 443 ssl;
server_name foo.bar.com
ssl_protocols TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_session_cache shared:SSL:10m;
ssl_certificate foobar.crt;
ssl_certificate_key foobar.key;
add_header Strict-Transport-Security "max-age=31536000";
include includes/web-site;
include includes/files-site;
}
No effect. Any advise?
Maybe you just want to redirect non-ssl traffic to ssl server. if so, you can try this
server {
listen 80;
server_name foo.bar.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
refer https://serverfault.com/questions/250476/how-to-force-or-redirect-to-ssl-in-nginx

How to redirect from http://www.* to https://* in Nginx?

So I am trying to achieve 4 things:
support both ip-v4 and ip-v6
support letsencrypt ssl certificates (the acme-challenge location in http)
redirect www to non www
redirect http to https
I have come up with a config, but it seems not to work. I get a "page does not exist" when trying to access http://www.MY_DOMAIN.COM.
Due to the hsts setting, this does work after having visited the https non-www version once.
Note that I have ssl certificates for both the with and without www domain.
How can I achieve this / what am I doing wrong in my config:
# HTTP server
#
server {
listen [::]:80;
server_name MY_DOMAIN.COM www.MY_DOMAIN.COM;
location /.well-known/acme-challenge {
root /var/www/letsencrypt;
try_files $uri $uri/ =404;
}
location / {
return 301 https://MY_DOMAIN.COM$request_uri;
}
}
# HTTPS server
#
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.MY_DOMAIN.COM;
ssl on;
ssl_certificate /etc/letsencrypt/live/www.MY_DOMAIN.COM/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.MY_DOMAIN.COM/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/www.MY_DOMAIN.COM/fullchain.pem;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 5m;
ssl_stapling on;
ssl_stapling_verify on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
add_header Strict-Transport-Security "max-age=86400; includeSubDomains";
return 301 https://MY_DOMAIN.COM$request_uri;
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server ipv6only=on;
server_name MY_DOMAIN.COM;
ssl on;
ssl_certificate /etc/letsencrypt/live/MY_DOMAIN.COM/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/MY_DOMAIN.COM/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/MY_DOMAIN.COM/fullchain.pem;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 5m;
ssl_stapling on;
ssl_stapling_verify on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
add_header Strict-Transport-Security "max-age=86400; includeSubDomains";
root /var/www/MY_DOMAIN.COM;
index index.html;
}
Also, I do not find the copy-paste nature of the two server blocks very nice.
As #RichardSmith notes; I was not listening to the ipv4 version of the http://www variant. Hence, the redict was not triggered at all.
After fixing this, the setup is working.

How to integrate ceritifacte with nginx

I used letsencrypt to create sertificate for my website and I got following keys generated by certbot
cert.pem
chain.pem
fullchain.pem
privkey.pem
Following is my nginx conf file. I created public and private key sometime ago which are in followig file
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name ec2-54-244-132-69.us-west-2.compute.amazonaws.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
location / {
try_files $uri $uri/ =404;
}
location /coupons {
proxy_pass http://127.0.0.1:9000;
}
location /bot {
proxy_pass http://127.0.0.1:5000;
}
location /v1/coupons/images {
alias /home/ubuntu/coupon-engine/resources/public/storage/img/;
}
}
I recommend this configuration. It's work for me.
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name ec2-54-244-132-69.us-west-2.compute.amazonaws.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security max-age=15768000;
location / {
try_files $uri $uri/ =404;
}
location /coupons {
proxy_pass http://127.0.0.1:9000;
}
location /bot {
proxy_pass http://127.0.0.1:5000;
}
location /v1/coupons/images {
alias /home/ubuntu/coupon-engine/resources/public/storage/img/;
}
}
I wrote some documentation for my self, you can see in my blog http://docs.harapan.me/2016/07/konfigurasi-secure-https-pada-web.html , but I am soory that the language in Indonesia

Resources