Question: How do I serve content from a proxy and static content from well just have nginx serve it up.
Background: Hello I'm setting up a really simple nginx application and for the most part it works but I'd like to better understand nginx what's going on. I've followed the documentation but in the real world I run into some weeds.
here's the config:
server {
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8080;
}
}
The above code works the thing is I have some static content that I'd just like nginx to serve w/o bothering to send that back to the proxy.
Directory structure:
/var/www/env/application/app/ <--this is the proxied location
/var/www/env/application/app/static <-- i'd like to just have nginx serve these files w/o the proxy.
When I edit my server settings and try to add:
location /static {
root /var/www/env/application/app/static/;
}
everything breaks
What am I doing wrong here?
You don't understand how root works. Look at the documentation again and then try to understand why it should be:
location /static {
root /var/www/env/application/app;
}
Related
I've read that it's not necessary to use sites-enabled, and even seen it suggested not to use.
In any case, its merit is not part of the question (so please consider that discussion off topic).
What I'm trying to do is set up an absolutely barebones basic nginx.conf file which does some super basic use case stuff: various forms of redirection.
By my understanding, this conf should be enough:
http {
# default server
server {
root /var/www/html/production-site;
# reverse proxy for external blog, makes example.com/blog display the blog. helps with SEO.
location /blog/ {
proxy_pass https://example.some-external-blog.com/;
}
}
# dev server
server {
server_name dev.example.com;
root /var/www/html/dev-site;
}
}
Unfortunately, my example doesn't work. The proxy bit works, but subdomains don't seem to. I don't honestly believe that server_name does anything at this point.
So, how does one write a simple (no extras) nginx.conf file which will exemplify these super trivial functionalities (subdomains and reverse proxies)?
I tried your config at my sandbox VM. nginx refuses to start, and when I run nginx -t command (which is always a good idea after significant configuration change), it says:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] no "events" section in configuration
nginx: configuration file /etc/nginx/nginx.conf test failed
So I've added events {} line to the config. After that nginx successfuly starts and all is working as expected.
Another thing that I wouldn't skip is including mime.types file. So final minimal configuration would look as follows:
events {}
http {
include mime.types;
# default server
server {
root /var/www/html/production-site;
# reverse proxy for external blog, makes example.com/blog display the blog. helps with SEO.
location /blog/ {
proxy_pass https://example.some-external-blog.com/;
}
}
# dev server
server {
server_name dev.example.com;
root /var/www/html/dev-site;
}
}
Is it possible for me to have an nginx server's domain to contain slashes? For example, for the server's root url location to be https://example.com/apps/app1?
I have a server whose files need to be served from /opt/production/app1/public. My current nginx.conf configuration, which doesn't work, contains:
location /apps/app1 {
root /opt/production/app1/public;
}
But obviously, this doesn't work because my files aren't at /opt/production/app1/public/apps/app1. I would like for nginx to consider https://example.com/app/apps1 to be my domain, so that my nginx.conf can access content as so:
location / {
root /opt/production/app1/public;
}
Is this at all possible? If not (which I suspect is the case), is there a way to work around this w/o changing the url schema?
If I understand your question correctly, you can try an alias directive for /apps/app1/ location:
location /apps/app1/ {
alias /opt/production/app1/public/;
}
I'm using Docker to serve my simple WordPress website. A nginx container and a wordpress container. Simple setup:
upstream wordpress_english {
server wordpress_en:80;
}
server {
listen 80;
server_name my_domain.com www.my_domain.com;
location / {
proxy_pass http://wordpress_english;
}
}
Problem: Static files (css, js and images) are not loaded.
The output from the browser console shows a 404:
http://wordpress_english/wp-content/themes/twentyfifteen/genericons/genericons.css?ver=3.2
Its easy to spot the problem: The browser looks for the static files at wordpress_english (the nginx upstream name), instead of my_domain.com
How can I fix this problem?
This is not a nginx problem, but a WordPress problem.
Solution:
In wp-config.php, add the following two lines:
define('WP_HOME','http://my_domain.com');
define('WP_SITEURL','http://my_domain.com');
During the WordPress installation, WordPress automatically sets WP_HOME to nginx upstream name. The above solution overwrites the default setting.
Seems to be an issue in your nginx config file.
When declaring your server my_domain you provide location / with proxy_pass wordpress_english. I don't know a lot on nginx, but I don't see any declaration of path in your server my_domain and is root is linked to wordpress_english. Seems normal that he is looking for files in wordpress_english and not in you server. (In fact, I guess he is looking in your server but your server tells to look in wordpress).
Not sure about it cause I don't know well nginx and proxy_pass functions.
I have a Play application listening on a local port :9000. There are other applications running.
I would like to server this application at a path like:
http://myhost/this-play-app -> localhost:9000
So that other apps could be nested at other paths.
I've tried the basic proxy_pass but it doesn't seem to work.
server {
listen 80;
server_name myhost;
# MMC Tool
# ----------------------------------------------------
location /this-play-app {
proxy_pass http://localhost:9000;
}
}
The play app seems to forward to the root. Is there a way to trick the play app to work within the /this-play-app path ?
Like /this-play-app/some-controller instead of /some-controller ?
Thanks
Using apps in folders isn't comfortable idea - you would need to at least prepare some dedicated config and change it each time when changing the location.
Instead as other suggested you should use subdomains, in this case each app behaves exactly the same as in the root domain and even if you will need/want to change that domain all you'll need will be change in the nginx's config.
Typical nginx's config looks like
upstream your_app {
server 127.0.0.1:9000;
}
server {
listen 80;
server_name your-app.domain.com;
location / {
proxy_pass http://your_app;
}
}
Most probably on some VPS or shared hosts you'll need to add the subdomain by some kind of admin's panel - on localhost just need add the subdomain to the hosts file.
Edit if using subdomain is not possible anyway (pity) you can workaround it anyway by config, in nginx use (as you did in question:
...
location /this-play-app {
proxy_pass http://your_app;
}
...
and then add this line into your application.conf (Play 2.1+)
application.context = "/this-play-app"
Or this in case of Play 2.4+ (info)
play.http.context = "/this-play-app"
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