NGINX, Include in my virtual host file - nginx

I have a DV server with MediaTemple and recently had their support enable ngnix webserver. I have been integrating their ProCDN with Super Cache on the WordPress sites on the DV.
I noticed on this domain convoyofhope.eu that the CDN is working properly, but if you view the site on Firefox the fontface isn´t working because of the cross-domain issue. I found this site that seems to solve the problem http://www.red-team-design.com/firefox-doesnt-allow-cross-domain-fonts-by-default
My question is, in the site it says:
Also, if you are using nginx as your webserver you will need to include the code below in your virtual host file:
location ~* \.(eot|otf|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}
I am just not sure where I put this on my server. I checked the vhost for convoyofhope.eu but I didn´t see where I would add that to make this work. Thanks for any feedback.

It would generally go in the nginx configuration file that has the server block for that host:
server {
listen 80;
server_name convoyofhope.eu;
...
location ~* \.(eot|otf|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}
...
}
On RHEL related distributions, this would be on the file system somewhere under /etc/nginx/. Your particular distribution may vary.

On MediaTemple, in your Plesk control panel, go to Websites & Domains (tab) -> Web Server Settings then scroll down to "Additional nginx directives". Place your location… directive in the text box there.

Related

Configure Nginx to proxy pass to Gunicorn (working when I visit IP, not working when I visit domain name)

I am trying to configure Nginx to proxy pass to Gunicorn.
My django project can be found at /home/justin/project/jobzumo
Start by creating and opening a new server block in Nginx’s sites-available directory:
sudo nano /etc/nginx/sites-available/jobzumo
Within this file I've entered the following:
server{
listen 80;
server_name 142.93.184.125;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/justin/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
When I go to 142.93.184.125 I see the default django rocket ship, so I think that means everything is working. However, when I go to 'jobzumo.com' (associated domain), I see the default 'Welcome to nginx!' page.
I know I have both the IP and domain name in my ALLOWED_HOSTS settings and have pointed the domain nameservers at my server. So, do I need to add this domain to this file? The tutorial I was following said either or should do the job. If adding the domain to this file is not what I have to do, can you mention that, so at least I know I'll have to start looking elsewhere. Thanks for any help.
You probably still have the default site in available sites in nginx which is causing the issue. I just had the same problem and the following two commands solved the issue:
sudo unlink /etc/nginx/sites-enabled/default
sudo service nginx restart
if you stopped your gunicorn daemon you might need to restart it and then run the second command above it should do the trick.

How do I set up an nginx conf with a subdomain and reverse_proxy and NO use of sites-enabled?

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;
}
}

Wordpress & Nginx with Docker: Static files not loaded

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.

Nginx Proxy for PlayFramework from port to path prefix

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"

Nginx: Prevent direct access to static files

I've been searching for a while now but didn't manage to find anything that fits my needs. I don't need hotlinking protection, as much as I'd like to prevent people from directly accessing my files. Let's say:
My website.com requests website.com/assets/custom.js, that'd work,but I'd like visitors which directly visit this file to get a 403 status code or something. I really have no idea if it's possible, and I don't have any logical steps in mind..
Regards !
You can use nginx referer module: http://nginx.org/en/docs/http/ngx_http_referer_module.html.
Something like this:
server {
listen 80;
server_name website.com;
root /var/www/website.com/html ;
location /assets/ {
valid_referers website.com/ website.com/index.html website.com/some_other_good_page.html ;
if ($invalid_referer) {
deny all;
}
}
}
This config guard assets directory. But remember, that not guaranteed and worked only for browser - any body can emulate valid request with curl or telnet. For true safety you need use dynamic generated pages with dynamic generated links.
You do not need to create the variable $invalid_referer as this is set by the nginx module.
If you nginx powered development instances are showing up in Google search results, there is a quick and easy way to prevent search engines from crawling your site. Add the following line to the location block of your virtualhost configuration file for the block that you want to prevent crawling.
add_header X-Robots-Tag "noindex, nofollow, nosnippet, noarchive";
You can simply deny access to any folder or file just by putting these lines with your folders' name
location ~ /(no_access_folder|folder_2)
{
deny all;
return 403;
}

Resources