I am trying to add a subdomain of my site. I have http://www.myweb.com/app/ and I just want http://app.myweb.com/ witch is the same of /app.
The content of /app is served by express in nodejs.
server {
listen 80;
server_name .myweb.com;
access_log /var/log/nginx/myweb.log;
location / {
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 Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:8888;
proxy_redirect off;
proxy_http_version 1.1;
}
}
And in nodejs I have:
http.use('/app', express.static(__dirname + '/static/app'));
But not sure if the statics should be served by nginx or nodejs/express
Searching in the web how use subdomain.
upstream your_app_name {
server localhost:3000;
keepalive 8;
}
server {
listen 80;
server_name ~^(?<sub>.+)\.domain\.com$;
location ~* \.html {
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://your_app_name/$sub;
proxy_redirect off;
}
}
Sorry; my english its very bad
Related
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'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'm trying to map routes by docker host, for each route, call a different container.
I have a docker-compose with 2 services, and that services is in :5000 port. My nginx.conf is mapped following below code:
location /template-api {
rewrite ^/template-api/?(.*) /$1 break;
proxy_pass http://template-api:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
location /api-plan {
rewrite ^/api-plan/?(.*) /$1 break;
proxy_pass http://api-plan:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
When, I call localhost:8000/api-template/documentation, the route that calls a static file, or route that returns a static file. He is returning error in localhost:8000/swaggerui, with file is not found.
That erros happens because the swagger ui folder is in localhost:8000/api-template/swaggerui and localhost:8000/api-plan/swaggerui
To fix that error, I'm add to ngix, the conf to map / route:
server {
listen 8000;
access_log /var/log/nginx/api.log;
error_log /var/log/nginx/api_error.log;
server_name localhost;
location / {
proxy_pass http://api-plan:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
Now when I call localhost:8000/api-plan/documentation, results in success, but, when I call localhost:8000/template-api/documentation, the API redirects to localhost:8000/api-plan/ resulting in wrong route.
Try this:
server {
listen 8000;
access_log /var/log/nginx/api.log;
error_log /var/log/nginx/api_error.log;
server_name localhost;
location / {
proxy_pass http://$server_name:8080/swaggerUi;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
location /template-api {
rewrite ^/template-api/?(.*) /$1 break;
proxy_pass http://template-api:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
location /api-plan {
rewrite ^/api-plan/?(.*) /$1 break;
proxy_pass http://api-plan:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
}
You can use cookies to detect which backend should receive the /swaggerui request. This is highly insecure since users can edit the cookie and try to reach other hosts but since it's a local test environment it just works.
server {
listen 8000;
access_log /var/log/nginx/api.log;
error_log /var/log/nginx/api_error.log;
server_name localhost;
location ~ ^/swaggerui {
resolver 127.0.0.11 ipv6=off;
proxy_pass http://$cookie_origin:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
location /template-api {
rewrite ^/template-api/?(.*) /$1 break;
proxy_pass http://template-api:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
add_header Set-Cookie "origin=template-api;Domain=localhost;Path=/;Max-Age=100000";
}
location /api-plan {
rewrite ^/api-plan/?(.*) /$1 break;
proxy_pass http://api-plan:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
add_header Set-Cookie "origin=plan-api;Domain=localhost;Path=/;Max-Age=100000";
}
}
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/;
}
}