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 ?
Related
I have 3 Linux servers (CentOS) like below;
____________________________________________________
| Name | Server | Network | IP |
------------+----------+-----------+----------------
| server0 | nginx | public | public IP |
| server1 | http | private | 192.168.1.1 |
| server2 | http | private | 192.168.1.2 |
------------+----------+-----------+----------------
Say I have a website example.com which hits server0. I have apache http server running in both server1 and server2. I have all my html pages present inside htdocs directory in server1. When user request https://example.com/page1.html or https://example.com/page2.html, it should be delivered from server1. This is working fine.
I have some downloadable files like file1.zip and file2.zip in server2. Inside htdocs directory of server2, I have another sub-directory downloads and inside that, there are files file1.zip and file2.zip. I can access these files from my private network using the url http://192.168.1.2/downloads/file1.zip or http://192.168.1.2/downloads/file2.zip.
The requirement is, I want to access those files using url https://example.com/downloads/file1.zip and https://example.com/downloads/file2.zip. My existing nginx configuration (nginx.conf) is like below;
http {
upstream server1 {
server 192.168.1.1:80 fail_timeout=0;
}
server {
server_name example.com;
root /usr/share/nginx/html;
location /download_files {
rewrite ^/download_files(.*)$ http://192.168.1.2/$1 redirect;
}
location / {
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-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://server1;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
The above configuration is working fine for delivering html pages from server1, but I am getting timeout while trying to access download urls. Why is this happening and how to fix this?
I solved it by modifying the config like below;
http {
upstream server1 {
server 192.168.1.1:80 fail_timeout=0;
}
upstream server2 {
server 192.168.1.2:80 fail_timeout=0;
}
server {
server_name mydomain.com;
root /usr/share/nginx/html;
location /download_files {
rewrite ^/download_files(.*)$ http://192.168.1.2/$1 redirect;
}
location /download_files {
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-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://server2;
}
location / {
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-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://server1;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
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.
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?
I currently have a node/angular app that runs as expected when pointed directly to the port configured (8081 for the purposes of explaining my situation). I'm able to post,get,put,delete as expected.
My goal is to have the node application running at mydomain.com/subfolder. When nginx is configured with the location of '/', everything works as expected. Config below:
upstream app_yourdomain {
server 127.0.0.1:8081;
}
server {
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
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_set_header X-NginX-Proxy true;
proxy_pass http://app_yourdomain/;
proxy_redirect off;
}
}
As soon as I change the location to /subfolder, however, my get,post,put,delete requests return 404 responses. The index.html configured in the node application is returned though. Configuration below:
upstream app_yourdomain {
server 127.0.0.1:8081;
}
server {
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
location /subfolder {
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://app_yourdomain/;
proxy_redirect off;
}
}
In my angular factory, I have my requests structured like return $http.get('/subfolder'); or return $http.post('/subfolder', {data: data});.
And, within my node application, I have the routes defined like app.get('/subfolder', somefunction); or app.post('/subfolder', somefunction);
Again, when I have the application running from the root of the domain, it works fine. When I have it configured to be in a subfolder of the domain, however, the requests no longer work.
My end goal would be to have multiple node applications running from sub-folders of a main domain. I've been fighting with this for a while, and found several articles for hosting mutliple node apps on a single server, but they seem geared toward having separate domains. I'd like (if possible) for these to run as separate apps for the same domain.
Any thoughts/tricks/pointers? Thanks!
Modify Your Nginx File to look like this:
upstream node{
server 127.0.0.1:3000;
}
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
location /node {
rewrite /node(.*) $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://node;
proxy_redirect http://node/ /node;
}
I got the above from here: http://skovalyov.blogspot.com/2012/07/deploy-multiple-node-applications-on.html and it works for me