Its been 4 hours of struggle and internet digging and I can't seem to understand why this Nginx configuration doesn't work.
AIM:
I have two completely different projects which I would like to host on the same domain using subdomains. So project one would be one.example.com while project two has to be two.example.com. I have also set two different node js servers sitting on port 4000 and 4001 and would like to have project one routed to 4000 and project two to 4001.
server {
listen 80;
server_name one.example.com;
location / {
proxy_pass http://127.0.0.1:4000/;
}
}
server {
listen 80;
server_name two.example.com;
location / {
proxy_pass http://127.0.0.1:4001/;
}
}
Used Command: service nginx start
And the error I get
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
In order to fix my issue I had to go to /etc/nginx/nginx.conf and add this line to the top of the http server block: server_names_hash_bucket_size 64;
Related
I'm having a bit of an issue accessing my nginx/gunicorn webapp server from outside of my local network. I'm not entirely sure what's wrong with my configuration, but I've spent hours on trial and error based on similar questions asked on stackoverflow with no success.
Here is my nginx configuration:
server {
listen *:80;
listen [::]:80;
server_name _;
location /static {
alias /home/pi/wl_app/wl_/website/static;
}
location / {
proxy_pass http://127.0.0.1:8000/;
include /etc/nginx/proxy_params;
proxy_redirect off;
}
}
And here is the configuration for supervisor, which runs gunicorn:
[program:wl_app]
directory=/home/pi/wl_app/wl_
command=/home/pi/wl_app/wl_/lift-venv/bin/gunicorn -w 9 main:app -b 0.0.0.0:8000
autostart=true
stopasgroup=true
killasgroup=true
stderr_logfile=/var/log/wl_app/wl_app.err.log
stdout_logfile=/var/log/wl_app/wl_app.out.log
My firewall is currently turned off and my ISP is not blocking the port I'm trying to use. Please let me know if there is any helpful information I can provide.
To be clear, the application works fine on my mobile device when I'm connected to the same network through wifi, but I cannot access the site if I switch to mobile data.
Thanks in advance for any hints anyone may be able to provide.
I'm a newbie at Nginx, and have been searching a lot for the right answer to my question, but couldn't find it; not because it is not there, but my newbie condition limits me to adapt a generic solution to my issue.
The situation is this:
I have a Mantis Bug Tracker in my private LAN (http://10.111.111.12).
On the other hand, i have an OwnCloud website also on my LAN (IP 10.111.111.5), with URL http://10.111.111.5/owncloud/.
What i want to do is to deploy a Nginx Reverse Proxy that handles all requests from Internet at publicdomain.com, and use trailing slash for each internal webserver. The desired result would be:
http://www.publicdomain.com/bugtracker -> redirects to http://10.111.111.12/index.php
http://www.publicdomain.com/cloud -> redirects to http://10.111.111.5/owncloud/ (note that "cloud" is preferred over "owncloud")
On the future, it is necessary to continue using trailing slash for other web servers to be deployed.
Questions are:
is this scenario possible? if so, is it enough with configuring nginx or I have to reconfigure internal web servers as well?
I really appreciate your help, by indicating me a possible solution or pointing me to the right direction on previous posts.
Thanks a lot in advance.
Yes it is possible to achieve such configuration and it's commonly used when NGINX is acting as a reverse proxy. You can use this configuration as an inspiration for building your own:
upstream bugtracker {
server 10.111.111.12;
}
upstream cloudupstream {
server 10.111.111.5;
}
server {
listen 80;
location /bugtracker/{
proxy_pass http://bugtracker;
}
location /cloud/{
proxy_pass http://cloudupstream/owncloud;
}
}
What's happening over here is that nginx will be listening on port 80 and as soon as a request comes for path /bugtracker, it will automatically route the request to the upstream server mentioned above. Using this you can add as many upstream servers and location blocks as you want.
Reference: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
Thanks a lot Namam for your quick answer. However, it isn't working yet. It seems that "server" at upstream directive does not allow slash, like this:
server 10.111.111.5/owncloud;
If i used it, i obtained
nginx: [emerg] invalid host in upstream "10.111.111.5/owncloud" in /etc/nginx/nginx.conf:43
I started with the first upstream bugtracker, only, and nginx.conf like this:
upstream bugtracker {
server 10.111.111.12;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location /mstic{
proxy_pass http://bugtracker;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
After that, when accesing to my Nginx Reverse proxy http://10.111.111.10/mstic/ i obtain the following:
Not Found The requested URL /mstic/ was not found on this server.
and no further details on error or access logs.
Thanks a lot in advance for any extra help you could bring me.
I try to test load balancer with nginx so I add upstream like the documentation exemple. But when I try to reload nginx to refresh the configuration, the refresh fail.
I just added upstream bloc, if I remove it all work
file : /etc/nginx/sites-available/default
upstream backend {
server webserver1:80;
server webserver2:80;
}
server {
listen 80;
listen [::]:80;
server_name www.interceptlocalcall.io interceptlocalcall.io;
location /users {
proxy_pass http://127.0.0.10:18000;
}
...
when I reload with the upstream block :
[FAIL] Reloading nginx configuration: nginx failed!
Nginx version :
nginx version: nginx/1.6.2
I see anything about this error, so I think I just miss something, perhaps in the install ?
If you have any idea thanks.
Please test your configuration by running $ nginx -c /etc/nginx/nginx.conf -t — it will tell you more detailed information about what's wrong with your configuration.
These messages also written to global error.log file (not one you define for servers but one defined in http section), usually located in /var/log/nginx/error.log.
Below simple nginx config for cluster, then I turn off 192.168.1.77:3032 server.
From time to time I catch 502 error and "no live upstreams while connecting to upstream client" in logs, while "server unix:///var/tmp/site.sock backup;" working and as I guess must handle request but nginx don't find it as live. What could be the problem?
nginx config:
upstream uwsgicluster {
server 192.168.1.77:3032;
server unix:///var/tmp/site.sock backup;
}
server {
listen 80;
server_name site.com www.site.com;
access_log /var/log/nginx/sire.log;
error_log /var/log/nginx/site-error.log;
location / {
uwsgi_pass uwsgicluster;
include uwsgi_params;
}
}
If I remove 192.168.1.77:3032 server
from upstream and restart nginx it works fine, but with switched off 192.168.1.77:3032 server errors occurs periodically
I think that nginx will still try both of the servers in the upstream block even if one isn't working. When it fails to connect to one of them, it will try the other one, but will still log the error you are seeing.
By default, the proxy_next_upstream setting will try the next upstream server on error or timeout. You can override this:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream
Are you only seeing error logs, or are you also seeing undesired behavior/load-balancing?
I got a new slice off slicehost, for the purposes of playing around and learning nginx and more about deployment generally. I installed a ruby app on there (which i'll call app1) which uses passenger. I made it the default app to use for that server with the following server block in my nginx config:
server {
listen 80;
server_name <my server ip>;
root <path to app1 public folder>;
passenger_enabled on;
}
This works fine. However, i want to try a few different apps out on this slice, and so thought i would set it up like so:
http:///app1
http:///app2
etc. I thought i would be able to do that by adding a location block, and moving the app1 specific stuff into it like so:
server {
listen 80;
server_name <my server ip>;
location ^~ /app1 {
root <path to app1 public folder>;
passenger_enabled on;
}
}
However, on doing this (and restarting nginx of course), going to the plain ip address gives the 'welcome to nginx' message (which i'd expect). But, going to /app1 gives an error message:
404 Not Found
The requested URL /app1 was not found on this server.
This is distinct from the error message i get when i go to another path on that ip, eg /foo:
404 Not Found
nginx/0.8.53
So, it's like nginx knows about that location but i've not set it up properly. Can anyone set me straight? Should i set up different server blocks instead of using locations? I'm sure this is simple but can't work it out.
Cheers, max
What you're after is name virtual hosting. The idea is that each domain is hosted on the same IP, and nginx chooses the virtualhost to serve based on the Host: header in the HTTP request, which is sent by the browser.
To use name virtual hosting, use the domain you want to serve instead of your server's IP for the server_name directive.
server {
listen 80;
server_name app1.com;
location / {
root /srv/http/app1/public;
passenger_enabled on;
}
}
Then, to host more apps on the same box, just declare a separate server { } block for each one.
server {
listen 80;
server_name app2.com;
location / {
root /srv/http/app2/public;
passenger_enabled on;
}
}
I'm using unicorn instead of passenger, but the vhost part of the structure is the same for any backend.
The global nginx config (which on its own hosts nothing): https://github.com/benhoskings/babushka-deps/blob/master/nginx/nginx.conf.erb
The template wrapper for each virtualhost: https://github.com/benhoskings/babushka-deps/blob/master/nginx/vhost.conf.erb
The details of the unicorn virtualhost: https://github.com/benhoskings/babushka-deps/blob/master/nginx/unicorn_vhost.common.erb
I fail to see the real problem here tho,
in order for you to figure that out
you need to view the nginx log files on most systems at:
/var/log/nginx/
and open the relevant access file here(might be error.log)
in there you can see what url nginx exactly tried to access and why did it fail.
What I really think is happening, that you got the root path wrong,
maybe it should be alias instead because
if you are proxifying the connection to another app, it might get the
"app1" word in the url instead of a direct one.
so please try:
server {
listen 80;
server_name <my server ip>;
location /app1 {
alias <path to app1 public folder>;
passenger_enabled on;
}
}
and see weather it works and also try to view the logs first to really determine whats the problem.
I think its just a slight syntax problem:
location ~ ^/app1 { ...
should work, or a little more efficient:
location = /app1 { ...
One problem is that your Rails app probably wasn't designed to run from a subdirectory. Passenger has a directive that will fix this:
passenger_base_uri /app1;
However, running Rails apps in subdirectories is somewhat non-standard. If you can, a better option may be to set up subdomains using nginx's virtual hosts.
It seems that you want to host more apps on the same server with base uri. Try this:
root /srv/http/;
passenger_base_uri /app_1;
passenger_base_uri /app_2
Also under /srv/http, create 2 symlinks:
ln -s /srv/http/app_1 /srv/http/app1/public
ln -s /srv/http/app_2 /srv/http/app2/public
The app1 can be accessed under: http://domain.com/app_1.
Here is more for reading: http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rack_to_sub_uri