I have a docker that run my api on localhost:80 and 2 folder for my fronts.
Here is what I want:
If I visit api.example.com map it to localhost:80
If I visit admin.example.com map it to folder ~/admin
If i visit example.com map it to folder ~/front
How can do this?
I found my answer and thanks from #Patrick-Mevzek.
I solved my problem by adding below server blocks to my nginx configuration.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/erfantahvieh.com/front;
server_name domain.example www.domain.example;
index index.html index.htm index.nginx-debian.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
autoindex on;
autoindex_exact_size off;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
root /home/erfantahvieh.com/admin;
server_name admin.domain.example www.admin.domain.example;
index index.html index.htm index.nginx-debian.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
root /usr/share/nginx/html;
server_name api.domain.example www.api.domain.example;
index index.html index.htm index.nginx-debian.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://xxx.xxx.xxx.xxx:8080/;
}
}
Related
I am working on a Ruby on Rails project on an Ubuntu server. Whenever I try and access the app, I am always greeted with:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
Here is my code from the "sites_enabled" directory:
pstream unicorn_site {
server unix:/tmp/unicorn.site.sock fail_timeout=0;
}
server {
listen 80;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 #503;
server_name http://[ip_address];
root /data/site/current/public;
try_files $uri/index.html $uri #unicorn_site;
location #unicorn_site {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn_site;
# limit_req zone=one;
access_log /var/log/nginx/site.access.log;
error_log /var/log/nginx/site.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
I am not sure what the problem is as the app seems to have everything it need to be working correctly. I can provide any part of the code anyone needs. Help is much appreciated.
EDIT: I am also always getting this error when I attempt to load the page:
2018/06/15 15:39:10 [warn] 15280#0: server name "http://[ip_address]" has suspicious symbols in /etc/nginx/sites-enabled/site:14
EDIT 2: Here is nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Move 'try_files $uri/index.html $uri #unicorn_site;' under location /
location / {
try_files $uri/index.html $uri #unicorn_site;
}
The server_name http://[ip_address]; looks wrong. try server_name example.com
Have you configured ruby_passenger line ?
Follow this carefully and your deployment should be successful
Choose your ubuntu version there and proceed
https://gorails.com/deploy/ubuntu/16.04
I've got a problem with proxying of subdomain's name from nginx to rails server. In my rails app I have links like tenant1.localhost:3000, tenant2.localhost:3000, etc. and it works fine. On production I use Nginx + Puma and nginx doesn't proxy to puma any request if I open link with subdomain.
nginx.conf
upstream puma_muninn {
server app:3000;
}
server {
listen 80;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 #503;
server_name localhost puma_muninn;
server_name ~^(?<subdomain>.+)localhost$;
root /var/www/muninn/public;
try_files $uri/index.html $uri #puma_muninn;
location #puma_muninn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://$subdomain.puma_muninn;
# limit_req zone=one;
access_log /var/www/muninn/log/nginx.access.log;
error_log /var/www/muninn/log/nginx.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
location ~ \.(php|html)$ {
return 405;
}
}
production.rb
config.action_dispatch.tld_length = 2
But as I said puma doesn't even get requests from nginx.
Any ideas?
A domain in nginx.conf has to be specific instead of localhost.
When a location is with root and another is with proxy_pass nginx does not work in the url /laravel. The response of this url is "404 Not Found".If I remove url location / and /moda, the url /laravel works. I do this, because I want map docker containers.
nginx.conf file :
server {
listen 80;
server_name local.monllar.com;
location /laravel {
root /var/www/local.monllar.com/public_html;
index index.html index.htm;
}
location / {
proxy_pass http://localhost:32768;
}
location /moda {
proxy_pass http://localhost:2222/moda;
}
}
I found the solution. This maps the ips of the docker containers to my local server names
nginx.conf file :
server
{
listen 80;
server_name local.monllar.com;
location / {
root /var/www/local.monllar.com/public_html;
index index.html index.htm;
}
}
server
{
listen 80;
server_name local.moda.com;
location / {
proxy_pass http://localhost:2222/moda/;
}
}
server
{
listen 80;
server_name local.laravel.com;
location / {
proxy_pass http://localhost:32768;
}
}
/private/etc/hosts file in Mac
127.0.0.1 local.monllar.com
127.0.0.1 local.moda.com
127.0.0.1 local.laravel.com
Using Rails 4.3. I have the following line in production.rb:
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
config.action_controller.asset_host = "https://assets.example.com"
And the following in example.conf nginx:
upstream example {
server unix:/home/deployer/example/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 503 504 /500;
root /home/deployer/example/current/public;
try_files $uri/index.html $uri.html $uri #example;
location #example {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://example;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
I notice that my subdomain http://assets.example.com is the mirror of my main domain https://example.com, instead of just serving the assets. For example, https://assets.example.com/blog/1 is the same for https://www.example.com/blog/1.
How do I prevent that? I just want https://assets.example.com to serve static assets.
I think that you should split your nginx server section into two sections, one for the web, the other one for the static assets site.
The server section for the web should remain essentially the same as in your original post, it should perhaps just react on the full hostname:
server_name www.example.com;
The static site server section should be an amended copy of the main site section, with the following principal differences:
the server_name should contain assets.example.com
the section should contain NO proxy_pass directive, static assets are meant to be served statically, i.e. directly by nginx, without having to go through Rails
the root will be the same as in the main site
the asset files will be served directly from the root, relatively, so if there is /assets/ in your URLs (which there probably is), then the assets should be found and served directly from your public/assets physical directory.
Update: sample nginx configuration:
upstream example {
server unix:/home/deployer/example/shared/tmp/sockets/puma.sock fail_timeout=0;
}
# main site config
server {
listen 80;
server_name www.example.com;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 503 504 /500;
root /home/deployer/example/current/public;
try_files $uri/index.html $uri.html $uri #example;
location #example {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://example;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
server {
listen 80;
server_name assets.example.com;
client_max_body_size 4G;
keepalive_timeout 10;
root /home/deployer/example/current/public;
location = /404.html {
root html;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
simple question, How can I deploy my Rails Application into a port of my website? I know i can specify the port when running using Mongrel or Webrick, but this time, I have to deploy it into production. I think passenger can manage this but I dont know how. I tried search but still I can't find the solution. Please help :)
Thanks!
Follow-up:
I am using Ubuntu 10.04 LTS and my Passenger runs with Apache.
If you're using Passenger with Apache or nginx. It will use the default port, 80. You can change this in the config file based on which web server you use.
# HTTPS server
server {
listen 80;
listen 8080;
server_name *.host.com;
root /home/app/public_html/host_production/current/public;
error_page 500 502 504 /500.html;
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
error_page 503 #503;
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
try_files $uri /system/maintenance.html #passenger;
location #passenger {
passenger_enabled on;
passenger_min_instances 5;
rails_env production;
}
if ($request_method !~ ^(GET|HEAD|PUT|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location ~ \.php$ {
deny all;
}
access_log /dev/null;
error_log /dev/null;
}
nginx+passenger config