app.somename.local is not working, but localhost is proxies to app:3000. I want to access to app via typing server_name into browser
worker_processes 1;
events {
worker_connections 1024;
}
http {
upstream app {
server app:3000;
}
server {
server_name app.somename.local;
location / {
proxy_pass http://app/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
}
}
}
app.somename.local
Your shooting at wrong target. You need to get that server name resolved.
Do this.
127.0.0.1 app.somename.local >> /etc/hosts
And it should work.
Related
I have nginx server and it's works fine for http requests. But on https it's not working.
How to solve it? Where is problem?)
upstream coinbot {
server web:5000;
}
server {
listen 80;
location / {
proxy_pass http://coinbot;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
server {
listen 443 ssl;
server_name coinbot;
ssl_certificate /etc/nginx/ssl/coinbot.crt;
ssl_certificate_key /etc/nginx/ssl/coinbot.key;
location / {
proxy_pass https://coinbot;
}
}
after a deployment tonight my nginx server keeps getting 301 redirecting to the same URI and thus browsers won't load the site. It's a node app within a docker container with 4 containers in total: app, wordpress, redis, nginx. I am pretty sure that I didn't change anything in the nginx config within the docker project, just changed the sub project (src-folder). I even reverted all my changes back to what I had before but the issue stays.
Neither the normal URL nor the Wordpress domain is loading, it will always 301 redirect to the root.
The configuration should 301 redirect all traffic from non-SSL to SSL and render the page.
My config looks like this:
upstream node {
server app:3000;
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com;
}
server {
listen 80;
server_name wordpress.example.com;
return 301 https://example.com;
}
server {
listen 443;
server_name www.example.com;
ssl_certificate /etc/nginx/ssl/example.com.cert;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
return 301 https://example.com;
}
server {
listen 443 default ssl;
server_name example.com;
root /var/www/html/project;
ssl_certificate /etc/nginx/ssl/example.com.cert;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
set $isopen 0;
location / {
proxy_pass http://node;
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 Accept-Encoding "";
proxy_set_header Proxy "";
expires off;
}
location /lp/ {
proxy_ssl_server_name on;
proxy_pass https://sub.example.com/folder/;
}
}
server {
listen 443 ssl;
server_name wordpress.example.com;
ssl_certificate /etc/nginx/ssl/wordpress.example.com.cert;
ssl_certificate_key /etc/nginx/ssl/wordpress.example.com.key;
error_page 397 https://$host:$server_port$request_uri;
set $isopen 0;
if ($request_uri ~ '^/wp-json/wp/v2/.*') {
set $isopen 1;
}
if ($request_uri ~ '^/wp-includes/.*') {
set $isopen 1;
}
if ($request_uri ~ '^/wp-content/.*') {
set $isopen 1;
}
if ($request_uri ~ '^/wp-admin/.*') {
set $isopen 1;
}
if ($request_uri ~ '^/wp-login.php') {
set $isopen 1;
}
if ($request_uri ~ '^/wp-admin/.*\.(php|css|js)') {
set $isopen 1;
}
if ($isopen !~ 1) {
return 301 https://$server_name/wp-admin/;
}
location / {
proxy_pass https://wordpress;
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 Accept-Encoding "";
proxy_set_header Proxy "";
expires off;
}
}
My docker containers listen to the following ports:
Containers statuses
Name Command State Ports
------------------------------------------------------------------------------------------------------------------------
project_app_1 bash -c npm install && npm ... Up
project_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
project_web-srv_1 nginx -g daemon off; Up 0.0.0.0:44301->443/tcp, 0.0.0.0:8080->80/tcp
project_wordpress_1 docker-entrypoint.sh apach ... Up 443/tcp, 80/tcp
Network status
Network name Network subnet Network gateway
project_network 172.100.112.0/24 172.100.112.1
Container name Container IPv4 address
project_wordpress_1 172.100.112.50/24
project_web-srv_1 172.100.112.30/24
project_app_1 172.100.112.40/24
project_redis_1 172.100.112.20/24
Hope somebody can help me. Thanks in advance.
I need to redirect or proxy_pass the following:
Every request starting with /api/v1/ to: #server
everything else / to #client
I have #server running on port 8080 and #client on port 8081
#client and #server are running as docker containers.
Note. Everything should be using https.
The following config is what I have but its not working idkw
server {
server_name example.com;
listen 80;
listen [::]:80 ipv6only=on;
return 301 https://example.com$request_uri;
}
server {
server_name example.com;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
...
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;
location /api/v1/ {
try_files $uri #server;
}
location / {
try_files $uri #client;
}
location #client {
proxy_pass http://client:8081;
}
location #server {
proxy_pass http://server:8080/api/v1/;
}
}
If you must use named locations then you can use error_page approach below.
By returning unused HTTP status codes and error_page for those codes set to named locations, we can forward requests to those named locations:
server {
server_name example.com;
listen 80;
listen [::]:80 ipv6only=on;
return 301 https://example.com$request_uri;
}
server {
server_name example.com;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
...
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;
error_page 350 = #client;
error_page 351 = #server;
location /api/v1/ {
return 351;
}
location / {
return 350;
}
location #client {
proxy_pass http://client:8081;
}
location #server {
proxy_pass http://server:8080/api/v1/;
}
}
nginx.conf (exposed on port 8900 in docker-compose)
upstream app_api {
server api:8888;
}
upstream app_dashboard {
server dashboard:8080;
}
server {
listen 80;
server_name api_server;
index index.html;
# Needed for Django API testing
location /static {
alias /var/www/static;
try_files $uri /$uri /index.html;
}
#Django API
location /api/ {
proxy_pass http://app_api/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
# Dashboard static served from nginx
location / {
root /var/www/dist/;
try_files $uri /$uri /index.html;
}
# Dashboard served from webpack-dev-server in container
location /serve {
proxy_pass http://app_dashboard;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
# still testing - this will fix the HMR for webpack
location /serve/sockjs-node {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://app_dashboard;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
vue.config.js
module.exports = {
baseUrl: process.env.NODE_ENV === 'docker' ? '/serve' : '/'
}
In a browser go to:
localhost:8900/serve
Pages get served fine, however,webpack-dev-server HMR is not working since the calls to sockjs are now /serve instead of /serve/sockjs-node :
POST http://localhost:8900/serve/908/3so2cyjm/xhr_streaming?t=1548885104223 404 (Not Found)
I am assuming this can be fixed with setting devServer.proxy instead of baseUrl, however I could not find the correct configuration.
What I need is:
/ -> should go to http://localhost:8900/serve
/api -> should go to http://localhost:8900/api
/sockjs-node -> should go to http://localhost:8900/serve/sockjs-node
We can change the default socket path using sockPath option:
module.exports = {
//...
devServer: {
sockPath: 'path/to/socket',
}
};
Here will be:
sockPath: '/serve/sockjs-node',
Managed to fix that issue with something like:
devServer: {
proxy: {
"/serve": {
target: "http://localhost:8900",
pathRewrite: { "^/serve": "/sockjs-node" },
},
}
}
I'd like to use nginx in order to map all my rails apps on port 80.
Currently, I have 3 rails apps running on port 3000 3001 and 3002 and I'd like to use nginx on port 80 to map them so :
http://127.0.0.1/app1 => 127.0.0.1:3000
http://127.0.0.1/app2 => 127.0.0.1:3001
http://127.0.0.1/app3 => 127.0.0.1:3002
Here's what I did :
server {
listen 80;
location /app1/ {
proxy_pass http://127.0.0.1:3000/;
}
location /app2/ {
proxy_pass http://127.0.0.1:3001/;
}
location /app3/ {
proxy_pass http://127.0.0.1:3002/;
}
}
However, when I try to access http://127.0.0.1/app1, I only get the HTML content, no assets/js/css as the browser tries to get them from http://127.0.0.1/assets instead of http://127.0.0.1/app1/assets.
Is there a way to fix this?
Add ActionController::Base.relative_url_root = "/app1" to the end of your config/environment.rb of app1 (similarly for other two apps). This will make Rails add proper prefix to URLs.
If you don't want to mess with Rails config, you probably could force Nginx to go through all of your assets folder until it finds the one it needs, if I'm not mistaken it could be archived like this:
location /assets/ {
try_files /app1/$uri /app2/$uri /app3/$uri;
}
Please note that you must have different filenames for assets of different apps. That is already so if you are using asset pipeline everywhere, as it hashes file names.
UPD.
You can also try 'Referer'-based routing:
location /assets/ {
if ($http_referer ~* /app1) {
rewrite ^(.*)$ app1/$1 break;
}
if ($http_referer ~* /app2) {
rewrite ^(.*)$ app2/$1 break;
}
if ($http_referer ~* /app3) {
rewrite ^(.*)$ app3/$1 break;
}
}
This is a good default configuration for what you want to achieve:
server {
listen 80;
location /app1/ {
root /srv/rails/app1/public;
try_files $uri $uri/index.html $uri.html #app1_forward;
}
location #app1_forward {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app1_backend;
}
location /app2/ {
root /srv/rails/app2/public;
try_files $uri $uri/index.html $uri.html #app2_forward;
}
location #app2_forward {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app2_backend;
}
location /app3/ {
root /srv/rails/app3/public;
try_files $uri $uri/index.html $uri.html #app3_forward;
}
location #app3_forward {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app3_backend;
}
}
upstream app1_backend {
server 127.0.0.1:3000 fail_timeout=0;
}
upstream app2_backend {
server 127.0.0.1:3001 fail_timeout=0;
}
upstream app3_backend {
server 127.0.0.1:3002 fail_timeout=0;
}
Also check this article, where I link to this example nginx config, for Rails.