I'm trying to use URL Rewrite with multiple directories in my Nginx but when I apply it I get an error 500.
server {
server_name mydns.com.br;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/godaddy-chained.crt ;
ssl_certificate_key /etc/nginx/ssl/server.key ;
root /var/www/manuais/index-site/;
location /folder1/ {
alias /var/www/manuais/folder1/;
rewrite /folder1/?(.*) /$1 break;
index index.html;
satisfy any;
allow myIP;
allow myIP;
deny all;
auth_basic "Auth";
auth_basic_user_file myAUTH;
}
location /folder2/ {
alias /var/www/manuais/folder2/;
rewrite /folder2/?(.*) /$1 break;
index index.html;
satisfy any;
allow myip;
allow myip;
deny all;
auth_basic "Auth";
auth_basic_user_file myAuth;
}
}
500 - Internal Server error
[alert] 2240016#2240016: *14432784 "alias" cannot be used in location "/folder1" where URI was rewritten, client: IP, server: mydns.com.br, request: "GET /folder1/ HTTP/1.1", host: "mmydns"
Is there any other way to rewrite this rule?
Related
I have the following nginx configuration, which is supposed to serve the index.html file to both www.domain.com and domain.com. While www.domain.com works, domain.com returns the default nginx welcome page.
What am I doing wrong?
server {
listen 80;
listen [::]:80;
server_name domain.com www.domain.com;
root /home/ubuntu/coming-soon-page/frontend/react_ui/build;
location = /favicon.ico {
access_log off; log_not_found off;
}
error_page 403 /home/ubuntu/coming-soon-page/frontend/react_ui/build/index.html;
location / {
root /home/ubuntu/coming-soon-page/frontend/react_ui/build/;
index index.html;
try_files $uri $uri/ /index.html;
}
location = /submit_user_feedback {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/coming-soon-page/backend/api/api.sock;
}
}
I am struggling to implement an automatic nginx redirect from non index pages to my index page, with the exception of /admin
For instance, example.com/test should redirect to example.com, but example.com/admin should not redirect to example.com
This is my current nginx configuration file:
upstream app_server {
server unix:/tmp/mysite.sock;
}
proxy_cache_path /var/www/example.com/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name www.example.com example.com;
# redirects both www and non-www to https
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/example.com/media;
}
location /static {
alias /var/www/example.com/static;
}
location / {
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
proxy_ssl_server_name on;
}
}
I have tried adding a try_files statetement within my location / block, and other things, but none seem to work. Am I missing something?
You are trying to mix proxy_pass with try_files, it won't work within the same location block. You can use named location instead and rewrite any URI that doesn't start with /admin to a root one using negative regex assertion:
location / {
try_files $uri #app;
}
location #app {
rewrite ^(?!/admin) / break;
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
}
You don't need the separate location /media { ... } or location /static { ... } blocks, because as nginx documentation states:
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}
Instead you just need to define the common server root (outside of any location blocks):
root /var/www/example.com;
You are also don't need to use the proxy_ssl_server_name directive since you are not proxying your request to the upstream with HTTPS protocol.
Here is my nginx.conf, works fine for https.
If someone types HTTP://dev.local.org:3002, how do I redirect to HTTPS://dev.local.org:3002 ?
This nginx is inside a docker-compose container.
worker_processes 1;
events {
worker_connections 1024;
}
#set $my_server_name _ #TODO global variable does not work?
http {
#DOCKER DNS - using this to resolve docker-compose hosts like 'appsearch', 'kibana' etc
resolver 127.0.0.11 ipv6=off;
#include mime.types;
default_type application/octet-stream;
#TO read external configuration
include sites-enabled/*.conf;
server { #DEFAULT SERVER
listen 443 ssl; # Security change
server_name _;
include common.conf;
include /etc/nginx/ssl.conf;
location / {
root html;
index index.html index.htm;
include common_location.conf;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# location /appsearch { #TODO this /appsearch did not forward. find how to do it.
# rewrite ^/appsearch(.*) /$1 break;
# resolver 127.0.0.11 valid=30s ;
# set $backend http://appsearch:3002;
# proxy_pass $backend; # Use variable To avoid upstream host not found error.
# }
}#server80
server {
listen 9200 ssl;
server_name _;
include common.conf;
include /etc/nginx/ssl.conf;
location / {
set $backend http://elasticsearch:9200;
proxy_pass $backend; # Use variable To avoid upstream host not found error.
include common_location.conf;
}
}#server
server {
listen 3002 ssl;
#server_name dev.local.org; #TODO yuck, bad to add server name!
server_name _;
include common.conf;
include /etc/nginx/ssl.conf;
location / {
set $backend http://appsearch:3002;
proxy_pass $backend; # Use variable To avoid upstream host not found error.
include common_location.conf;
}
}#server
server {
listen 5601;
server_name _;
include common.conf;
include /etc/nginx/ssl.conf;
location / {
set $backend http://kibana:5601;
proxy_pass $backend; # Use variable To avoid upstream host not found error.
include common_location.conf;
}
}#server
}
Use the 497 HTTP error to redirect: (source: https://meabed.com/http-497-status-code/)
In your conf you would add something like this:
listen 1234 ssl;
server_name your.site.tld;
ssl on;
error_page 497 https://$host:1234$request_uri;
}```
I am trying for hours to get WebDAV to work on Nginx on Debian Buster. I have created a directory /var/www/html/webdav that is owned by www-data (user/group). I am using nginx-full, that includes http_dav_module and http-dav-ext. I have tried endless combinations of different directories in different locations with different rights but I always get the same error:
2019/09/24 14:12:47 [error] 3065#3065: *1 directory index of "/var/www/html/webdav/" is forbidden, client: 192.168.1.132, server: _, request: "GET /webdav/ HTTP/1.1", host: "192.168.1.219"
Here is my nginx configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location /webdav/ {
client_body_temp_path /var/cache/nginx;
autoindex on;
autoindex_exact_size off;
auth_basic "restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
create_full_put_path on;
dav_methods PUT DELETE MKCOL COPY MOVE;
dav_ext_methods PROPFIND OPTIONS;
dav_access group:rw all:r;
}
}
What am I doing wrong?
I am unable to get my HTTP --> HTTPS rules working correctly in my NGINX config. In addition to HTTPS, I'm also trying to redirect WWW to NON-WWW. However, I can't get WWW to even work.
Here is where I'm at:
http://example.com --> https://example.com (WORKS)
http://example.com?id=3 --> https://example.com?id=3 (WORKS WITH QUERY STRING)
http://www.example.com -> https://example.com (DOESNT WORK)
https://www.example.com (DOESN'T WORK)
URL's with WWW show "Server not found. Firefox can't find the server at www.example.com."
server {
listen 80;
server_name www.example.com;
return 301 https://example.com$request_uri;
# this doesn't work. trying to http://www.example.com --> https://example.com
}
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
# this works. http://example.com -> https://example.com
}
server {
listen 443 ssl;
ssl on;
ssl_certificate /etc/sslmate/www.example.com.chained.crt;
ssl_certificate_key /etc/sslmate/www.example.com.key;
server_name www.example.com;
return 301 https://example.com$request_uri;
# this has www in server_name, and doesn't work
}
server {
listen 443 ssl;
ssl on;
ssl_certificate /etc/sslmate/www.example.com.chained.crt;
ssl_certificate_key /etc/sslmate/www.example.com.key;
server_name example.com;
root /home/garrett/domains/example.com/public_html;
access_log /home/garrett/domains/example.com/logs/access.log;
error_log /home/garrett/domains/example.com/logs/error.log;
index index.php index.html index.htm;
error_page 404 /404.php;
error_page 403 /404.php;
location / {
#try_files $uri $uri/ /index.php;
}
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm-garrett.sock;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
# this block works, because I can access https://example.com correctly
}
What am I doing wrong?