I'm trying to serve some static files (images) on nginx and I'm always getting 404.
This is the url I'm testing:
/uploads/test.png
I have the folder with the images in:
/root/project/server/uploads
This is my nginx configuration:
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name domain.com www.domain.com;
location ^~ /assets/ {
gzip_static on;
expires 12h;
add_header Cache-Control public;
}
location /uploads {
alias /root/trendcircle/server/uploads;
try_files $uri $uri/ =404;
autoindex on;
}
location /api {
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_set_header X-Forwarded-Proto $scheme;
proxy_set_header Access-Control-Allow-Origin '*';
proxy_set_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
proxy_set_header 'Access-Control-Allow-Headers' 'X-Requested-With, Accept, Content-Type, Origin, x-access-token';
proxy_pass http://localhost:8080;
}
location / {
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8081;
}
}
Can some one help me?
Thanks in advance!!
The folder didn't have the same user as the user running nginx. If it by executing this command:
chmod o+x /root
Related
500 internal server error
when checked the nginx container logs this is the error that was showing.
rewrite or internal redirection cycle while internally redirecting to "/index.html", client: x.x.x.x, server: *.domain.com, request: "GET / HTTP/1.1", host: "master.domain.com
In location /auth when mentioned localhost keycloak cannot be reached when localhost is replaced with ip in nginx config it works fine.
Below is the nginx config file
server {
listen 80;
listen [::]:80;
server_name domain.com;
#root /usr/share/nginx/html;
root /home/admin/newprodle/frontend/build/;
index index.html;
return 301 https://$host$request_uri;
}
server{
listen 443 ssl;
server_name *.domain.com domain.com;
ssl_certificate /etc/nginx/fullchain.pem;
ssl_certificate_key /etc/nginx/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:MD5;
root /home/admin/newprodle/frontend/build;
# root /usr/share/nginx/html;
index /index.html;
client_max_body_size 10M;
location / {
try_files $uri $uri/ /index.html;
}
location /auth {
proxy_pass http://localhost:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
}
location /api {
proxy_pass http://localhost:5000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
}
location \~ .(jpg|png|jpeg|gif|pdf|xlxs|xl|doc❘docx) {
add_header 'Access-Control-Allow-Origin' '*' always;
root /home/admin/newprodle/backend/src/uploads;
}
location /socket.io {
proxy_pass http://localhost:5000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
}
}
webpage screenshot
Nginx configuration:
server
{
listen 80;
server_name ****;
listen [::]:80 default_server ipv6only=on;
}
server
{
listen 443;
server_name ****;
location /
{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:8080/;
proxy_ssl_session_reuse off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
server
{
listen 443;
server_name ****;
location /
{
#root /var/www/tite_staging/public/www/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:8080/;
proxy_ssl_session_reuse off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
removed "return 302 https://$host$request_uri;" from http block because it causes a ERR_TOO_MANY_REDIRECTS error
May I know what the issue is here? thank you.
Running a javascript application with node server
server {
listen 80;
# catch all server names here
server_name _;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name tite.rdc.nie.edu.sg;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 443;
server_name staging.tite.rdc.nie.edu.sg;
location / {
proxy_pass http://localhost:8088/; # <--- SEE PORT HERE
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Assumptions:
URL tite.rdc.nie.edu.sg running application branch A on server A
URL staging.tite.rdc.nie.edu.sg running application branch B on server A
Means that you can not run two different applications on same server behind same port. During application start-up you should get error message, something like:
Bind address already in use
Also check /etc/nginx/config folder if it does not contain another (defaults) *.conf files
I am trying to serve my landing page and my Vuejs app separately.
Below is Nginx config for serving my VueJS app and it's working fine. How can I configure it to serve my landing page from "/" and my VueJS app from "/app"?
server {
listen $PORT;
root /usr/share/nginx/html;
index index.html index.html;
location / {
try_files $uri /index.html =404;
}
location ~ ^/(?:wizard_question|wizard_submit) {
proxy_pass http://127.0.0.1:5050;
proxy_http_version 1.1;
proxy_redirect default;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_set_header X-Forwarded-Host $server_name;
}
}
After you change your routes and assets paths, try the following config:
server {
listen $PORT;
index index.html index.html;
location / {
root /usr/share/nginx/landing;
}
location /app/ {
alias /usr/share/nginx/html/;
try_files $uri /app/index.html;
}
location ~ ^/(?:wizard_question|wizard_submit) {
proxy_pass http://127.0.0.1:5050;
proxy_http_version 1.1;
proxy_redirect default;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_set_header X-Forwarded-Host $server_name;
}
}
I have an express app with two routes: an index route (/) and an article route (/article).
Now I try to deploy this application on an Ubuntu server behind a Nginx.
The problem I have is that the index route available at https://servername/app works fine, however, if I am entering a route (starting from index) via the upstream /app is lost and I am routed to https://servername/article/param which clearly is not available as I would like to be routed to https://servername/app/article/param.
It works smoothly on my local computer where no upstream/Nginx is interposed.
My Nginx config looks as follows:
upstream app {
server localhost:3000;
}
server {
#listen [::]:80 default_server ipv6only=on;
#root /usr/share/nginx/html;
# index index.html index.htm;
# Make site accessible from http://localhost/
server_name example.test.at;
#Enable the verification for letsencrypt
location /.well-known {
root /usr/share/nginx/html;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# try_files $uri $uri/ noen;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.test.at/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.test.at/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location ~^\/(images|stylesheets|fonts|javascripts) {
expires 1M;
access_log off;
add_header Cache-Control "public";
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET;
add_header Access-Control-Allow-Headers X-Requested-With,content-type;
add_header Access-Control-Allow-Credentials true;
root /var/www/webviews/app/public;
}
location ^~ /app {
#rewrite ^/noen(/.*)$ $1 break;
proxy_pass http://app/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_set_header X-Forwarded-Proto $scheme;
}
}
server {
if ($host = example.test.at) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
server_name example.test.at;
return 404; # managed by Certbot
}
Do you see any issues in my Nginx config?
I missed a location section for the article, where the (.*) handles the param (subpath):
location ~^/article/(.*) {
proxy_pass http://noen;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_set_header X-Forwarded-Proto $scheme;
}
following is my nginx configuration,
server { //PART-1
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.1:8090;
proxy_redirect off;
proxy_pass_request_headers on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection Upgrade;
}
}
server { //PART-2
listen 80;
server_name service;
root /usr/local/tomcat7/webapps/service-snapshot;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/ServiceUI/;
}
}
first part of config works fine for websockets, which I am already using.
Second part of config is for webapp running on Apache tomcat 7.0.56, which is not working.
Is there something wrong with config? assuming server_name in both parts might be causing issue!
Any suggestions!
While having multiple services on one IP and port is working perfectly fine, the server_name directive is using the HOST header submitted by the client/browser. In this case, you're not supplying the header but instead asking for a specific location on the same server (you're not asking for http://_ or http://service but for http://yourserver/services from what I see in the comments).
To make it work, you have to specify the different services via locations like this:
server {
listen 80;
server_name THIS_IS_WHERE_YOUR_DOMAIN_OR_MAYBE_LOCALHOST_GOES;
location / {
proxy_pass http://127.0.0.1:8090;
proxy_redirect off;
proxy_pass_request_headers on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection Upgrade;
}
location /Service {
root /usr/local/tomcat7/webapps/service-snapshot;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/ServiceUI/;
}
}