Hi i have this nginx setup:
server {
listen 80;
server_name xxxxxx.xx www.xxxxxxx.xx;
root /home/xxxx/public_html;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
#proxy_redirect off;
#proxy_cache_bypass $http_upgrade;
}
}
My app creates folders inside the uploads/user followed by the user's ID and then stores images into it. Every user have his own folder inside uploads/user/xxxx.
ex.
/uploads/user/5878db663e67e3535a47c638/1085.github-logo.png
The app works fine in browser but when the user uploads the image it cannot be displayed so a 404 rises.
what can i do?
You can use try_files to serve up static content (if it exists) and otherwise defer to NodeJS:
root /home/xxxx/public_html;
location / {
try_files $uri $uri/ #proxy;
}
location #proxy {
proxy_pass http://127.0.0.1:3000;
...
}
See this document for details.
Related
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've nginx config that route frontend app to specific port, and backend to specific route
here's my configs
server {
listen 80;
server_name test.com www.test.com;
location / {
proxy_pass http://127.0.0.1:8080;
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;
}
location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
}
}
when trying to access test.com directly it works, but when trying to access test.com/api , it doesn't work, then if return to main path test.com it also doesn't work, it seems like nginx stopped working after accessing the /api
I use nginx Nginx as web server, and project build use Nodejs. There are 2 Project, one run on port 6000 and another on port 7000.
Every request api.mysite.com will redirect to project 1 (localhost:6000), and request api.mysite.com/v2 will redirect to project 2 (localhost:7000).
But,I want request api.mysite.com/oauth/token redirect to localhost:7000/oauth/token
In the server block , I have configure like this
upstream mysiteapiv1 {
server localhost:6000;
}
upstream mysiteapiv2{
server localhost:7000;
}
server {
listen 80;
server_name api.mysite.com;
root /var/www/html/v1;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://mysiteapiv1;
}
location /v2 {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://mysiteapiv2;
}
location /oauth/token {
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header Host $http_host;
proxy_pass http://localhost:7000/oauth/token;
}
location /public/ {
try_files $uri $uri/ =404;
}
}
But request api.mysite.com/oauth/ resulted not found
What should I do ?
I have nginx works as a reverse proxy for a node.js express application.
I can access to
http://ip:8081/data/emoji.json
but it doesn't work with domain name
http://domain_name/data/emoji.json
doesn't work.
Here is the configuration of nginx:
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.html index.htm index.nginx-debian.html;
server_name domain_name;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
proxy_pass http://127.0.0.1:8081;
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;
}
location /mongo {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
rewrite /mongo/(.+) /$1 break;
proxy_pass http://127.0.0.1:28017;
break;
}
}
}
I'm using Nginx as a reverse proxy to route requests to localhost to my node server, but requests to localhost/blog to a ghost server (a blogging platform)
My Nginx config is like this:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
location ^~ /blog {
rewrite /blog/(.*) /$1 break;
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_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:2368;
proxy_redirect off;
}
}
edit: The reason I'm rewriting the URL with rewrite /blog/(.*) /$1 break; is so that the ghost server doesn't get the request to /blog/, which it can't use
However because ghost, the blogging platform, is designed to be run in the root directory, all static assets fail because they should be going to localhost/blog/static/asset/path rather than localhost/static/asset/path:
Is there any simple solution to this problem? Or do I have to rewrite all the HTML for ghost?