Renaming next's output folder from "_next" to "next" - next.js

I came across use-case today where I was trying to get my nextjs app working in android webview, unfortunately android doesn't resolve paths that begin with _next and most of my chunk files are under _next/static when I build my app.
Is there a way to to rename this folder from _next to next or anything else?
This issue will give extra context about how android works in this regard Android project is not resolving any static assets

I handled this issue via Nginx Routing.
What i assumed while making this Nginx Routing is that every file will have unique hash and file name combination.
This is the snippet of code of Nginx Routing which i used.
location /static/ {
try_files $uri #server1;
}
location #server1{
proxy_pass http://192.168.1.1$uri;
proxy_intercept_errors on;
recursive_error_pages on;
error_page 404 = #server2;
}
location #server2{
proxy_pass http://192.168.1.2$uri;
proxy_intercept_errors on;
recursive_error_pages on;
error_page 404 = #server3;
}
location #server3{
proxy_pass http://192.168.1.3$uri;
}
What the above code does is, whenever Nginx encounters /_next/ it will first try that URL with #server1, if that Server responds with 404, the Nginx then transfers the request to #server2 as mentioned in error_page param.
This example works like:
try_files $uri #server1 #server2 #server3
This worked for me as i had 2 apps which were developed on Next and both has _next route.
Let me know if this solves your problem.

Related

Can I configure nginx with proxy_pass to use an error document on the proxied server?

I've configured nginx with proxy_pass to proxy URLs like /uploads/foo.png to fetch from an S3 bucket, but obviously missing files result in ugly XML errors, and I want to return a static HTML file.
I tried using the "static website" feature of S3, but it always returns (incorrect) 403 status codes with the error doc, and it seems there's no way to alter that via proxy_pass.
For various reasons, using a local file on the nginx server isn't an option. This nginx instance only does proxying.
Can I have nginx re-request an error document from the proxied S3 bucket?
Yes. First place 404.html in your S3 bucket. Then configure your location block like this:
location /uploads {
proxy_pass YOUR_S3_BUCKET_URL;
proxy_intercept_errors on;
error_page 403 404 /uploads/404.html;
}
proxy_intercept_errors will tell nginx to handle error codes in the proxied response.
The error_page will handle the S3 error code by internally changing the request to /uploads/404.html
And finally your existing location /uploads block will fetch that 404 doc from the proxied server!
Presumably you could even fetch the error doc from a different origin:
location /uploads {
proxy_pass ORIGIN_A_URL;
proxy_intercept_errors on;
error_page 403 404 /error-docs/404.html;
}
location /error-docs {
proxy_pass ORIGIN_B_URL;
}

Nginx: protect all but one url

I have try a few examples but all return almost the same, I want to password protect everything after .com/, problem is there are no "static" files, the server is Tomcat with nginx, the app is build on play framework.
The whole idea is that everything has to be password protected except for one url.
.com/service/contact {has to be protected}
.com/categories/service/contact {protected}
.com/service/jp/ {protected}
.com/service/jp/allo {Public not protected}
server {
...
auth_basic "Restricted Access"
auth_basic_user_file path_to_basic/password_protection
location ~^/service/jp/allo/ {
auth_basic off;
allow all;
satisfy all;
try_files $uri $uri/ =404;
}
}
With that the first part is working which is to protect everything, but not the second part which to leave one url as public, in my case is returning a 404 error, not a custom 404 but an internal default 404.The url's are "virtal" no actual folder exist, the server is using rewrite for seo url.

NGINX unexpected behaviour with location directive and proxy_pass

I have a NGINX configuration file to serve a Website with static files and via a development Server.
static -> http://localhost:8080
dev webserver -> http://localhost:8080/dev
There are several other services which I bind to different location directives.
Here is a snipped of the configuration file.
...
upstream qgis {
server qgis-spcluster_server:80;
}
...
server {
listen 8080;
server_name localhost;
location / {
root /usr/share/nginx/html/build;
index index.html index.htm;
auth_basic "Zugangskontrolle";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /dev/ {
proxy_pass http://web_app/;
auth_basic "Zugangskontrolle";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /static/ {
proxy_pass http://web_app/static/;
}
location /qgis/ {
proxy_pass http://qgis/;
}
location /apex/ {
proxy_pass http://apex/apex/;
auth_basic "off";
}
...
Everything works as expected until i open the URL to get the static files. After that all other URLs leads to the static files.
http://localhost:8080/apex -> Apex Service
http://localhost:8080 -> static Website
http://localhost:8080/apex -> static Website
For me everything looks ok, but indeed something is not ok.
The Basic_Auth produce another unexpected behaviour.
http://localhost:8080 -> basic auth -> success -> static website
http://localhost:8080/apex -> basic auth -> it is not possible to get rid of the pop up
So in the moment I'm a little bit clueless how to solve this issue.
Please remove the trailing / from your location directives or provide / when you access them.
Nginx looks for the longest prefix match location. When you access http://localhost:8080/apex, it's routed to / because /apex/ is not the prefix of /apex
Documentation of location is here
I tried now several things, but nothing really worked well. So I decided to create a second server block for all location directives which are problematic with my current setup.
Probably this is not the best solution, because I have still no idea why I get these problems. But it works now and that counts for me.

Nginx - how to return a proxy generated file?

Description:
I want to implement an http server (using nginx) that serves static files.
If the requested file doesn't exist, nginx shall send a request to a service (REST API) that will create the file and return its path.
After that, I want nginx to return the static file that was created.
Question:
What is the best way to return the file after its creation?
So far I managed to do this by changing the REST API in order to return the created file path with the 302 status code and with a location header as a redirect, but I am not sure if this is a good thing to do. Is it?
Is there any nginx-side solution for this? Do I have to create a custom module?
Conf file:
http {
server {
listen 80;
location /files {
try_files $uri #rest;
}
location #rest {
rewrite ^(.*)$ /api/ break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8080;
}
}
}
Edit: Actually, on balance, this should be even simpler:
location #rest {
...
proxy_intercept_errors on;
error_page 404 = $uri;
}
Configure the named location to intercept an "error" coming back (I chose 404), and then using the error_page directive will cause the given URI to be loaded again. Since the file now exists, the request should succeed.
Side note: I had thought try_files $uri #rest $uri would have worked, but an internal redirection only happens for the last argument.
The simplest option here is probably for your REST service to use X-Sendfile/X-Accel to return the relevant URI that Nginx should serve once the file is created. Your REST service could return the target URI using the header X-Accel-Redirect.
In your case, your API could actually just return the same URI it received as the X-Accel-Redirect header, and then Nginx would re-use the same location block and find the file for the subrequest occurring.
If this fails, however, using an internal Nginx location as per the examples at http://wiki.nginx.org/XSendfile and http://wiki.nginx.org/X-accel:
location /files-protected {
internal;
root /path/to/files;
}
and returning the relevant URI to that would also work.

Nginx only serves images, but not css or js, Rails 3.1

Here's my conf for the website on Rails 3.1:
server {
listen 80;
server_name test.mysite.com www.test.mysite.com;
root /var/www/mysite/test/current/public;
location ~* ^/assets/ {
expires max;
add_header Cache-Control public;
break;
}
passenger_enabled on;
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
if (-f $document_root/maintenance.html) {
rewrite ^(.*)$ /maintenance.html break;
}
}
Only images are rendered though. css and js show 404 page. What am I doing wrong?
Apparently, I haven't payed enough attention. Those css and js files were not actually compiled for some reason and, thus, have not appeared in assets/public dir. At least not all of them: for instance application.css is there, but other css files are not. Anyway, that is not an nginx problem, so I'm marking this as the right answer.
I'd like to share with you my configuration on Github of nginx+passenger in production mode. it resolves you problem with server static content right way
I had the same issue, one of my stylesheets was not being compiled, but this guide provided me with a solution.
I added my custom.css to config/environments/production.rb this way...
config.assets.precompile += ['custom.css']
After that I issued bundle exec rake assets:precompile and my problem was solved.

Resources