Remove query string from nginx - nginx

I'm having some trouble with an nginx.conf. I have a front end application which appends this code to the URL after a user logs in. I'm trying to have nginx remove this ?code=randomcode. I tried writing an nginx rewrite rule, but it is conflicting with another rule I have in place it seems. Below is the nginx.conf file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri$args $uri$args/ /index.html;
location /app1 {
try_files $uri$args $uri$args/ /index.html;
rewrite ^/app1(/.*) $1;
break;
}
location /app1/ {
set $args '';
rewrite ^/app1/(.*)$ /app1/$1 permanent;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
}
I am new to writing directives for nginx config files, so the logic may not make sense, but basically with the last rewrite rule written furthest down is causing all the JS files to report 404 (Not Found).
Any advice would be appreciated.

Related

I get nginx error - location 404 not found

When I try www.abc.com it works but when i try www.abc.com/def or www.abc.com/ghi/ get 404 error.
Here is my nginx config
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.abc.com;
location / {
proxy_pass http://I/;
}
location /def{
proxy_pass http://oid-global-windows/;
}
location /ghi/ {
proxy_pass http://oid-global-windows/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
I tried to tweak the config with alias instead of proxy_pass still no luck. BTW, oid-global-windows is a docker container name and nginx is in another container hosted in same windoes server.
The setup I followed was as per this link -
https://www.mihajakovac.com/run-nginx-webserver-in-docker-on-windows/

Block specific file out of static directory with nginx

I have a statically served React application with Nginx.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 3000;
server_name localhost;
root /usr/share/nginx/html;
location ^~ /static/ {
root /usr/share/nginx/html/;
}
location / {
try_files $uri /index.html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
During build (with Vite) we produce source maps which are served in development.
For specific environments (stage.* and app.* but not dev.*) I would like to not serve the source maps.
I have no option to not build source maps for these environments in the first place, because we migrate entire builds between environments.
I want to use Nginx to not serve *.map files out of /static for these environments.
How would I do that?

How to redirect nginx 404 error_page to homepage?

Here is an example of my server block:
server {
listen 80;
server_name website.com;
root /opt/bitnami/nginx/html/website;
index index.php index.html index.htm;
error_page 404 = #homepage;
location #homepage {
return 302 /;
}
location / {
try_files $uri $uri $uri/ #extensionless-php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
include "/opt/bitnami/nginx/conf/bitnami/phpfastcgi.conf";
include "/opt/bitnami/nginx/conf/bitnami/bitnami-apps-prefix.conf";
}
What happens is that pages that returned 404 still return 404 error, no redirect happends. What I'm trying to acomplish is to redirect 404 errors to homepage (or at least any other).
My nginx.conf file.
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
proxy_intercept_errors on;
error_page 400 500 404 /;
}
}
}

nginx location 404 not found

Here are my nginx configure files.
On the default.conf, the first location is used to access /usr/share/nginx/html directory, it is ok while I access http://47.91.152.99.
But when I add up a new location for directory /usr/share/nginx/public directory, nginx return me a 404 page while I access http://47.91.152.99/test.
So, what is the matter? Am I misuse the directive of nginx?
/etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ^~ /test/ {
root /usr/share/nginx/public;
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 /usr/share/nginx/html;
}
}
The following erroneous block (in your case);
location ^~ /test/ {
root /usr/share/nginx/public;
index index.html index.htm;
}
is telling nginx to look for directory 'test' into the folder (root) /usr/share/nginx/public. If there's no 'test' folder in that root, it will return 404. To paliate to your problem, i suggest you try using alias instead of root. Like so:
location ^~ /test/ {
alias /usr/share/nginx/public;
index index.html index.htm;
}
Also, just for kicks, index directive can be set generally so you don't have to re-write it all the time... like so;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
error_page 500 502 503 504 /50x.html;
location / { }
location ~^/test/ {
alias /usr/share/nginx/public;
}
location = /50x.html {
root /usr/share/nginx/html;
}
}
One thing you should also consider... the more 'precise' the location block, the higher in your config it should reside. Like that location = /50x.html. In a perfect world, that would be set up top, right after the general server block settings.
Hope it helps.
Error caused by root directive
location ^~ /test/ {
root /usr/share/nginx/public;
index index.html index.htm;
}
Fix with alias directive
location ^~ /test/ {
alias /usr/share/nginx/public;
index index.html index.htm;
}
Other Improvements
Extra tip: the index directive can be set so that you don't have to re-write it.
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
error_page 500 502 503 504 /50x.html;
location / { }
location ~^/test/ {
alias /usr/share/nginx/public;
}
location = /50x.html {
root /usr/share/nginx/html;
}
}
nginx matches Location blocks partly based on position in the config. Ideally, you would invert what you have now. The location block would be higher in nginx config. To that end, the location = /50x.html would also move up. Order is
Exact match =
Forward match ^~ /
Case sensitive regex ~ /
Case insensitive regex ~*
Path match /
More about nginx location priority. Also, you can always review the official documentation. The nginx documentation for location block http://nginx.org/en/docs/http/ngx_http_core_module.html#location
when your app is vuejs,you need write like this,can prevent 404,pay attention to double /test/
location ^~/test/ {
alias /usr/local/soft/vuejs/;
try_files $uri $uri/ /test/index.html;
}
I just solved this (index.html not found) issue.
For me, I misstyped my project name to match your ec2 project name with the nginx path.
Move to nginx/sites-enabled to check nginx path
cd /etc/nginx/sites-enabled
cat your_project_name
Check your nginx path
(for me : root/home/ubuntu/practice/current/public;)
Move to home directory to check your project name
4. cd
5. ls
6. If your ec2 project name(for me: practice) is not match with your nginx path name(for me: practice) then you might got "index.html not found error"
and my server-blocks.conf
server {
listen 80;
index index.html index.htm index.nginx-debian.html;
server_name 13.xxx.xxx.xx;
location / {
root /var/www/portaladmin/;
proxy_pass http://13.xxx.xxx.xx:80/;
}
error_log /var/log/nginx/portaladmin-erorr.log;
}
and my load-balancer.conf
server {
listen 80;
server_name xx.xxx.xxx.xx
access_log /home/ec2-user/logs/lb-access.log;
error_log /home/ec2-user/logs/lb-error.log;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://13.xxx.xxx.xx:80;
}
}
Creating nginx & sftp pods using Kubernetes.. I've found that the mountPath in my sftp-deployment.yaml file is related to the username in my sftp-server.
And the error has happened when I have changed the username without changing mountPath value to match my username. So, the files were uploading to '/home/old-username' instead of '/home/new-username'.

Directory listing: 403 Forbidden nginx

Hello I started using nginx, set up the nginx.conf to my project folder C: / Projects.
I used (AutoIndex: on) to have a list of directories, but does not work
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name localhost;
root C:\Project ;
index index.html index.htm index.php;
location / {
autoindex on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Move your autoindex on; outside your location block
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name localhost;
root C:\Project ;
index index.html index.htm index.php;
# moved here
autoindex on;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
If you have one of the index files present inside your root directory (or any subdirectory under C:\Project for that matter), the index page will be shown rather than the index of contents.
You may also want to check the permissions on all content under your root; they need to be readable by the user running your Nginx process.
Could be a number of different things; you'll need to clarify your questions in future with what you've tried and haven't tried.

Resources