I know there are quite a few threads on this topic, but unfortunately none of them helped me so far. I had an express application and served my static files like this:
app.use(express.static(path.join(__dirname, 'public'), { maxAge: 86400000 }));
Now I'm using nginx aswell and wanted to serve my static content only via nginx, so I commented the line above and added the following to my sites-enabled/default nginx config:
server {
listen 443 ssl;
// certificate, server_Name
location ~^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
root /path/to/my/project/public/;
access_log off;
expires 24h;
}
}
location / {
proxy_redirect off;
proxy_pass https://127.0.0.1:8443;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_cache_bypass $http_upgrade;
proxy_cache one;
proxy_cache_key sfs$request_uri$scheme;
}
From what I've read so far the first location block should handle the serving of static files (all my static files are located under public). But when I start my application like this I get ERR_TOO_MANY_REDIRECTS. Only once I add the express config from above my site works.
How can I serve static content via nginx correctly?
Does anyone have some guidance on what exactly I'm doing wrong?
Related
I'm serving multiple nodejs apps on a single server through pm2 and using nginx to manage reverse proxies. Right now if I use the server's ip and app port to reach the apps directly it all works fine. But if I try to navigate to my apps through the location paths set in the nginx config then I get 404 errors.
Below is my nginx default config:
upstream frontend {
server localhost:3000;
}
upstream backend {
server localhost:8000;
}
server {
listen 443 ssl;
server_name <redacted>;
ssl_certificate <redacted>.cer;
ssl_certificate_key <redacted>.key;
error page 497 301 =307 https://$host:$server_port$request_uri;
location /app/frontend {
proxy_pass http://frontend;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
}
location /api {
proxy pass http://backend;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
}
}
server {
listen 80;
server_name <redacted>;
return 301 https://$server_name$request_uri;
}
Now when I try to go to https://<server ip>:3000, the frontend loads just fine but if I go to https://<server ip>/app/frontend, I get the following 404 error:
Although the index.html loads up, it tries to find the static assets on https://<server ip>/ but rather should try to find them on https://<server ip>:3000. This is the exact behaviour that I'm trying to achieve.
What I have tried so far:
Using rewrites
Adding trailing slashes to both location path and proxy_pass
I know this can be solved by changing the app's base url or the build directory but that is not what I'm looking for.
Any help would be highly appreciated.
I have backend web-application working on 8081 port. It is Java spring boot application like uber-jar. So, static files placed into jar file. I have nginx as frontend. And I want to setting up cache static files on frontend. I thought what it is something like this:
proxy_cache_path /tmp levels=1:2 keys_zone=my_cache:10m max_size=10g
inactive=60m use_temp_path=off;
server {
listen 80;
server_name site.ru;
location /my_app {
proxy_pass http://127.0.0.1:8081;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /my_app/.+\.css {
proxy_pass http://127.0.0.1:8081;
proxy_cache my_cache;
proxy_cache_valid 200 1d;
}
}
... but it is not working. I believe what I on right way, though a little bit wrong
I am looking at building a website using node however my idea would be best with the use of subdomains. What i wanted to know is do i need to run each subdomain as a separate app and point to them with nginx?
Current Structure
- client
-- assets
--- js
--- css
--- img
- server
-- app
--- home
--- subapp1
--- subapp2
-- node_modules
-- server.js
-- config.js
The above structure is the thought that each app will run ny itself however they have common views that should be shared. Server.js will run them all. I just need to know if this is correct structure and if there is any cleaner or better alternative. Thanks
I'd suggest setting up Nginx to reverse proxy to 2 NodeJS apps. Run 1 app on port 8081 for example (service1) and the other on 8082 (service2) like this:
upstream service1 {
ip_hash;
server localhost:8081;
}
upstream service2 {
ip_hash;
server localhost:8082;
}
server {
listen 0.0.0.0:80;
server_name service1.mydomain.com;
location / {
index index.html index.htm;
proxy_pass http://service1;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 0.0.0.0:80;
server_name service2.mydomain.com;
location / {
index index.html index.htm;
proxy_pass http://service2;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I'm trying to host 2 applications in the same droplet in digitalocean by using nginx
But so far I've only been able to get the root application running (the one without example.com/secondapp)
I want to be able to not use a subdomain and just use example.com/secondmeteorapp to be able to access it.
My sites-enabled/default looks like this:
server {
listen 80;
#listen 443 ssl;
server_name example.com/;
#ssl_certificate /etc/nginx/ssl/ssl-bundle-myApp-domain-com.crt;
#ssl_certificate_key /etc/nginx/ssl/myApp_domain_com.key;
location /dragonfire {
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto http;
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
}
location /images {
alias /home/dragonfire-build/bundle/programs/web.browser/assets/images;
access_log off;
expires max;
}
location /fonts {
alias /home/dragonfire-build/bundle/programs/web.browser/assets/images;
access_log off;
expires max;
}
location "^/[a-z0-9]{40}\.(css|js)$" {
root /home/dragonfire-build/bundle/programs/web.browser;
access_log off;
expires max;
}
}
however, when I access http://serverIpAddress/dragonfire it can't find the css or javascript giving me this error:
GET http://myipaddress/1f3848edee9e199050b9b1965b9e697aa714b9f3.css?meteor_css_resource=true
GET http://myipaddress/6e48198c6b584ff79c86e0c624a65b4853faaf50.js?meteor_js_resource=true 404 (Not Found)
I can access the app if I go directly through the IP address and port but not via the nginx way
QUESTION
How can I access a second app using the same domain but with a /mysecondapp (in this case /dragonfire at the end?
My current nginx config is this:
upstream nodejs {
server 127.0.0.1:3000;
}
server {
listen 8080;
server_name localhost;
root ~/workspace/test/app;
index index.html;
location / {
proxy_pass http://nodejs;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I'm very very new to nginx, but at the very least I know that nginx is better than node/express at serving static files. How can I configure the server so that nginx serves the static files?
I solved it using this new configuration:
upstream nodejs {
server localhost:3000;
}
server {
listen 8080;
server_name localhost;
root ~/workspace/test/app;
location / {
try_files $uri #nodejs;
}
location #nodejs {
proxy_redirect off;
proxy_http_version 1.1;
proxy_pass http://nodejs;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Thanks to the following Stack Overflow post:
How to serve all existing static files directly with NGINX, but proxy the rest to a backend server.
You'll probably want another location block within your server for the static files.
location /static {
alias /path/to/static/files;
}
This uses the alias directive.
Then you can hit files at localhost:8080/static/some_file.css
P.S. You don't need the root or index that you have set currently.
(root is similar to alias with a slight difference in usage)