I have 2 html files in /var/files: host1.html and host2.html.
I want nginx serving any of them (as index file) depending on rails response, eg http://localhost - should render host1.html or host2.html.
This is Rails controller:
class HomeController < ApplicationController
def index
response.headers['X-Accel-Redirect'] = 'host1.html'
head :ok
end
end
And nginx config:
upstream app_server {
server 0.0.0.0:3000 fail_timeout=0;
}
server {
listen 8080 default;
location /{
proxy_pass http://app_server;
internal;
root /var/files/;
}
}
This is not working.
If remove internal - request proxied to Rails, but no internal redirect occurs...
Also, i want to serve not only html files - any actually.
Please advice.
Thanks.
I would use something like this
nginx config:
upstream app_server {
server 0.0.0.0:3000 fail_timeout=0;
}
server {
listen 8080 default;
location / {
proxy_pass http://app_server;
}
location /internal/ {
internal;
alias /var/files/;
}
}
Rails controller:
class HomeController < ApplicationController
def index
response.headers['X-Accel-Redirect'] = '/internal/host1.html'
head :ok
end
end
Here we define “virtual” folder /internal/ that will serve static files. When Rails “redirects” to /internal/host1.html nginx will match it to location /internal/, replace /internal/ with /var/files/ (see alias for details) and serve static file /var/files/host1.html. But if user points his browser to /internal/host1.html he will get 404 error due to internal directive.
Related
I have, what I hope is, a simple question. I am running Nginx and some applications in Docker containers. Some of the applications run on the same host as Nginx. I can access an application using, for example, app.example.com, but I want to access the same application using example.com/app. I cannot figure out how to define the server block with location /app. I would like to achieve something like:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass app-srv:port;
}
}
server {
listen 80;
server_name example.com;
location /app {
What do I place here?
}
}
Edit: with additional information.
My server configuration is:
server {
listen 80;
server_name openhab.aronica-sys;
location / {
proxy_pass http://openhab:8081;
}
}
server {
listen 80;
server_name aronica-sys;
location /openhab/ {
proxy_pass http://openhab:8081/;
}
}
openhab in the proxy_pass statements is the Docker virtual address for the openHAB server.
'openhab.aronica-sys' gets:
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/ui/tiles".
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/".
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/habot/greet".
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/ui/components/ui:widget".
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/ui/components/ui:page".
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/items?metadata=semantics,listWidget,widgetOrder".
VM6:1 XHR finished loading: POST "http://openhab.aronica-sys/rest/events/states/2e0eee99-770f-498b-bd9f-736777096c30".
VM6:1 XHR finished loading: POST "http://openhab.aronica-sys/rest/events/states/2e0eee99-770f-498b-bd9f-736777096c30".
aronica-sys/openhab gets:
VM6:1 GET http://aronica-sys/rest/ui/tiles 404 (Not Found)
VM6:1 GET http://aronica-sys/rest/ 404 (Not Found)
VM6:1 XHR failed loading: GET "http://aronica-sys/rest/ui/tiles".
aronica-sys/:1 Uncaught (in promise) Not Found
aronica-sys/:1 Uncaught (in promise) Not Found
VM6:1 XHR failed loading: GET "http://aronica-sys/rest/".
I do not know how to interpret the above information nor how to proceed.
You will need something like:
server {
listen 8080;
server_name app.example.com;
location / {
proxy_pass http://localhost:9090;
}
}
server {
listen 8080;
server_name example.com;
location /app/ {
proxy_pass http://localhost:9090/;
}
}
Please be careful with the slashes used in location and in proxy pass, they are quite important! For an explanation on the slashes, you can check: https://stackoverflow.com/a/51225241/83037
Edit: with the above configuration your actual sever process will receive the request without any modification to the host part. If you use in a browser http://app.example.com/file.html the server will receive a request for http://app.example.com/file.html (same host app.example.com). If you use in a browser http://example.com/app/file.html the server will receive a request for http://example.com/file.html (same host example.com, different path).
Generally, a server could "care" about the hostname, and/or could care "care" about the path, in the example above if it does not "care" about hostname will "interpret" the request as "give me /file.html", but if he "cares" about hostname, it will be configured to work only for a set of hostnames - server might answer or example.com but not for app.example.com.
Based on your addition, your server is configured to answer only to one specific hostname (he "cares" about hostname). In that case you can make nginx "rewrite" the incoming request such that your server "sees" only one hostname. Like if you use in a browser http://example.com/app/file.html the server will receive a request for http://app.example.com/file.html (notice example.com changed to app.example.com).
The configuration for that is:
server {
listen 8080;
server_name app.example.com;
location / {
proxy_pass http://localhost:9090;
proxy_set_header Host app.example.com;
}
}
server {
listen 8080;
server_name example.com;
location /app/ {
proxy_pass http://localhost:9090/;
proxy_set_header Host app.example.com;
}
}
WARNING! The server can still show you links to the domain it is configured to. In your case, the server might show (or build with javascript) a link to "http://openhab.aronica-sys/my_page.html". If that is an issue for you, I would suggest to open another question - this question is about the virtual directory.
I'm trying to use nginx to reverse proxy some services like Kanboard, PgAdmin, StackStorm, Grafana, etc. Using location /kanboard/ and /pgadmin/ i'm redirected correctly but the page can't import js and css files. Obs: I am using docker for the services and nginx.
I've already tried some proxy_pass params but didn't succeeded.
Nginx config
# /etc/nginx/nginx.conf
events {}
http {
server {
listen 80;
location /kanboard/ {
proxy_pass http://kanboard;
}
location /pgadmin/ {
proxy_pass http://pgadmin;
}
}
}
Accessing localhost/kanboard i was expected to be redirected to localhost/kanboard/login, but i am redirected to localhost/login.
When i access localhost/pgadmin the js and css files fail to import on the network tab, all the headers are to localhost/static/... instead of localhost/pgadmin/static/...
I defined environment variables in nginx.conf like below -
server {
listen 80;
server_name XX.XX.XX.XX; //Masked for this question
location ~ ^/clients/abc(/.*|$) {
alias /home/abc/Project/public$1;
passenger_base_uri /clients/abc;
passenger_app_root /home/abc/Project;
passenger_document_root /home/abc/Project/public;
passenger_enabled on;
passenger_env_var AWS_U disha;
}
}
I restarted nginx but when I open rails c and type ENV['AWS_U'], it returns nil.
What possibly could I be doing wrong?
U need to move passenger_env_var directive to server section.
server {
...
passenger_env_var VAR value;
}
Environment variables are only set when the application gets loaded by Passenger. They are not set when the application starts by rails c command.
If you want to know the value of an environment varialbe on the running application loaded by Passenger, you can make a temporal action for debugging:
class DebugController < ApplicationController
def foobar
render plain: "FOOBAR = '#{ENV['FOOBAR']}'"
end
end
Rails.application.routes.draw do
get "/debug/foobar" => "debug#foobar"
end
I am using thin as my web server in rails application. While trying to hit a url http://localhost:3000/search/% thin is throwing
Invalid request: Invalid HTTP format, parsing fails.
How to write custom 400 response handler with thin?
Update nginx configuration file as below and create 400.html file in public folder.
server { listen 80;
root /public; # <--- be sure to point to 'public'!
error_page 400 /400.html;
location = /400.html {
internal;
}
location / {
return 400;
}
}
I've got a Rails app deployed via nginx/passenger. It will have multiple domains pointing to it.
I'm wondering if it's possible to configure nginx so that any URL that matches [somedomain.com]/blog/ will be servered by PHP/WordPress located in a different directory.
So, for example:
domain1.com, domain2.com, & domain2.com/some-resource/1 point to the Rails app at /var/rails/mainapp/
but domain1.com/blog/ goes to /var/sites/domain1.com/
and domain2.com/blog/ goes to /var/sites/domain2.com/
server {
location /blog {
alias /var/sites/domain1.com/;
}
location / {
}
}
You need define you /blog before / location
Here is my config. Hope it helps someone.
# Redirect all requests containing 'www.your-website.ru'
# to 'your-website.ru'
server {
listen 80;
server_name www.your-website.ru;
rewrite ^(.*) http://your-website.ru$1 permanent;
}
server {
listen 80;
server_name your-website.ru;
access_log logs/your-website.ru.log;
root /path-to-your-website.ru/current/public;
#####################
# Rails
#####################
location / {
rails_env production; # this is a production server
passenger_enabled on; #