Custom configuration

Hi !

I’m using NGinx since few weeks.
One of our customers wants his mp3 downloaded instead of being opened
by the browser.

.htaccess aren’t read, so I’ve decided to add a config file for his
domain, containing the “option” types, to add “mp3” as
“application/octet-stream”.

It’s working well, but his website is now ugly, because the
instruction also applies for .css O_O

root@shadow [/etc/cpnginx/custom]# cat domain.com
types {
application/octet-stream mp3;
}

Is it a normal behavious, did I miss something?

Thank you for your help !

Cheers, Adnan RIHAN.
Président-Fondateur de l’association Virtual-Info (Loi 1901),
hébergement Web et Serveurs de jeux.
Technicien-Supérieur développeur d’applications Web et Système.
3e année à {Epitech}.
Envoyé de mon iPhone 

On Tue, 2012-03-27 at 23:46 +0200, Adnan RIHAN wrote:

It’s working well, but his website is now ugly, because the
instruction also applies for .css O_O

root@shadow [/etc/cpnginx/custom]# cat domain.com
types {
application/octet-stream mp3;
}

When you use the types directive, it replaces all the types with
whatever you specify. You don’t define any mimetypes besides mp3, so
Nginx uses the default (application/octet-stream) for anything it
doesn’t recognize, which now includes css.

Try something like this instead:

location ~* .mp3$ {
types { application/octet-stream mp3; }
}

See also:
http://wiki.nginx.org/HttpCoreModule#types

As an aside, you could also just edit /etc/nginx/mime.types and change
the type for mp3 files.

Regards,
Cliff

I’ve done this:

root@shadow [/etc/cpnginx/custom]# cat mega-sound.be
location ~* .mp3$ {
types {
application/octet-stream mp3;
}
}

But, after rebuilding vhost and restarted nginx, the mp3 are currently
audio/mpeg (referring to firefox).
Is there a way to check if the config is correctly “set” ?

Thanks for your help.

Cordialement, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le mercredi 28 mars 2012 à 00:15, Cliff W. a écrit :

On Wed, 2012-03-28 at 07:16 +0200, Adnan RIHAN wrote:

audio/mpeg (referring to firefox).
Is there a way to check if the config is correctly “set” ?

Start by posting more of your configuration.

Cliff

I’m actually using NGinx as webserver, for a hosting service, with
CPanel. CPNginx, the name of the module.

root@shadow [/etc/cpnginx]# ls
./ data.conf localkey.txt uninstall.sh
…/ direct/ md5.txt version.txt
cpanelproxy.conf expires/ nginx.cont.template vhost.conf
custom/ fileextensions.txt php.ini

server_name cpanel.* www.cpanel.;
proxy_redirect http://www.cpanel.
:8888 http://www.cpanel.*;
access_log off;
proxy_busy_buffers_size 64k;
}
proxy_read_timeout 90;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
proxy_pass http://127.0.0.1:2077/;
location /whm-server-status {
access_log /usr/local/apache/logs/access_log;
proxy_pass http://127.0.0.1:8888/whm-server-status;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

http {
gzip_buffers 4 32k;
gzip_types text/plain application/x-javascript text/xml text/css;
ignore_invalid_headers on;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
include “/usr/local/nginx/conf/vhost.conf”;
include “/etc/cpnginx/cpanelproxy.conf”;
}

And finally

root@shadow [/etc/cpnginx]# cat vhost.conf
client_max_body_size 2000m;
client_body_buffer_size 512k;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 32k;
proxy_buffers 16 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_connect_timeout 30s;

In custom, there is only one file, containing the previous posted
config.

Thanks for your help.


Ragards, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le mercredi 28 mars 2012 à 08:02, Cliff W. a écrit :

Sorry but, did I paste enough config files ?

Is there an error ? :confused:

Regards, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le mercredi 28 mars 2012 à 09:48, Adnan RIHAN a écrit :

Adnan,

For the following to work you need headers module installed:

           location ~ /(?<filename>(?!/).+\.mp3)$ {
              types {
                application/octet-stream mp3;
              }
              add_header Content-Disposition "attachment;

filename="$filename"";
}

Kind regards,

Andrejs

Posted at Nginx Forum:

On Wed, 2012-03-28 at 09:48 +0200, Adnan RIHAN wrote:

Which vhost is the one in question? Can you paste only the relevant
server block? Most of what you pasted appears to be specific to running
cPanel (I don’t have any cPanel installs to check).

Also, you mention stuff in “custom”, but I don’t see where any of the
config you pasted includes a “custom” config.

Cliff

Sorry but, what do you mean? Isn’t “headers” a standard module ? How
to check the compiled modules please ?

nginx -V

Try my solution, it works for me.

Andrejs

Posted at Nginx Forum:

Which vhost is the one in question? Can you paste only the relevant
server block?

@Cliff: I didn’t find them :confused: I’ve asked the CPanel module devs to tell
me where are the vhosts. Actually, I think nginx is just a “proxy” to
apache, but it’s strange… It means apache is still running and
.htaccess working… I’ll tell you as soon as I have the response.

Also, you mention stuff in “custom”, but I don’t see where any of the
config you pasted includes a “custom” config.

My custom conf is in “/custom/”. This is what I had before @locojohn
tip.

root@shadow [/etc/cpnginx/custom]# cat mega-sound.be
location ~* .mp3$ {
types {
application/octet-stream mp3;
}
}

For the following to work you need headers module installed:
Sorry but, what do you mean? Isn’t “headers” a standard module ? How to
check the compiled modules please ?

location ~ /(?(?!/).+.mp3)$ {
types {
application/octet-stream mp3;
} add_header Content-Disposition “attachment; filename="$filename"”;
}

This is my new custom config file. Nothing changed. Still audio/mpeg :confused:


Cordialement, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le mercredi 28 mars 2012 à 20:31, locojohn a écrit :

@Cliff: Here is the customer’s vhost:

root@shadow [/etc/cpnginx/custom]# cat /usr/local/nginx/conf/vhost.conf | grep
domain.com
########### Vhost configuration of mega-sound.be
server_name domain.com www.domain.com;
access_log /usr/local/apache/domlogs/domain.com;
proxy_redirect http://www.domain.com:8888 http://www.mega-sound.be;
proxy_redirect http://domain.com:8888 http://domain.com;
proxy_redirect http://domain.com:8888 http://domain.com;
include “/etc/cpnginx/custom/domain.com”;

@locojohn: Here is the custom file:

root@shadow [/etc/cpnginx/custom]# cat domain.com
location ~ /(?(?!/).+.mp3)$ {
types {
application/octet-stream mp3;
}
add_header Content-Disposition “attachment; filename="$filename"”;
}

@locojohn: I’m using your custom config :confused: . Plus, here is my nginx -V:

root@shadow [/etc/cpnginx/custom]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.0.11
built by gcc 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-openssl=/usr/

When I access a .mp3, here is the answer:

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 28 Mar 2012 23:06:25 GMT
Content-Type: audio/mpeg
Content-Length: 8976405
Last-Modified: Mon, 26 Mar 2012 18:50:14 GMT
Connection: keep-alive
Expires: Thu, 29 Mar 2012 23:06:25 GMT
Cache-Control: max-age=86400
Accept-Ranges: bytes

Thank you for your help.


Cordialement, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le jeudi 29 mars 2012 à 00:57, locojohn a écrit :

Unfortunately, it doesn’t work.
Here is a link example: http://www.mega-sound.be/adata/428.mp3

Actually, the first thing i’ve tried, is:

types {
application/octet-stream mp3;
}

The only failure, is that with that, even CSS are downloaded (so, webste
screwed up), and now I know that I HAVE to use “location”.

I gave you an example link, the mp3 are in:
/home/customer/www/adata/428.mp3, they’re directly downloadable…

And poor me, the dev of CPNginx plugin told me that they don’t support
custom config, so they won’t help me T_T


Cordialement, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le jeudi 29 mars 2012 à 01:15, locojohn a écrit :

Adnan, if it doesn’t set the “Content-Disposition” header as per my
example, then it doesn’t match the location in which this header is set.
It is likely that some other location has preference and so the code I
provided will never be triggered. Look into your nginx config more
closely. Also, check the error log, do you get any warnings?
Sometimes, PCRE library may throw a warning, maybe you can use
alternative location syntax compatible with an older version of PCRE:


location ~ /(?P(?!/).+.mp3)$ {

Are your mp3 links static at all?

Andrejs

Posted at Nginx Forum:

Mmmh…

Ok. I see, maybe there will never be any match, as there is only
“proxy_redirect” in vhosts;

########### Vhost configuration of mega-sound.be
server_name mega-sound.be www.mega-sound.be;
access_log /usr/local/apache/domlogs/mega-sound.be;
proxy_redirect http://www.mega-sound.be:8888
http://www.mega-sound.be;
proxy_redirect http://mega-sound.be:8888 http://mega-sound.be;
proxy_redirect http://mega-sound.be:8888 http://mega-sound.be;
include “/etc/cpnginx/custom/mega-sound.be”;

Apache is still listening on :8888
Is there a chance with this conf, to match any “location” as there is no
“document_root” or equivalent ?
I mean, location is system based or URL based?

If it’s system, I don’t know. If it’s URL based, there should be a match
since the first test.
Isn’t it ?

Cordialement, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le jeudi 29 mars 2012 à 02:15, Cliff W. a écrit :

On Wed, 2012-03-28 at 19:15 -0400, locojohn wrote:

Adnan, if it doesn’t set the “Content-Disposition” header as per my
example, then it doesn’t match the location in which this header is set.
It is likely that some other location has preference and so the code I
provided will never be triggered.

I’m fairly certain this is exactly what is happening. Either solution
should work fine, but only if the location is properly matched.

Again, it’s not the preferred solution, but directly editing mime.types
would probably work.

Cliff

Hi,

Finally I need your help :confused:

The “CPNginx” devs have a website with a ticket support system, but
they’re telling me that they don’t support custom configuration…
I’ve told them I need their help because they know how they built their
module and how they made it work, they still just answer “You need to do
it by yourself”…

What can I do T_T ?

Regards, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le jeudi 29 mars 2012 à 02:36, Adnan RIHAN a écrit :

Hey everybody !

Here are some news.
I’ve compiled nginx with HttpHeadersMoreModule
(Headers More | NGINX), to add custom
headers in order to see if the location is or isn’t matched.

Here is what I tried:
URL: http://www.mega-sound.be/adata/428.mp3
Custom config file:

root@shadow [/etc/cpnginx/custom]# cat mega-sound.be
location ~* .mp3$ {
more_set_headers ‘X-MyHeader: .mp3’;
}

location /adata {
more_set_headers ‘X-MyHeader: adata’;
}

more_set_headers ‘X-MyMasterHeader: set’;

The headers sent:

Cache-Control: max-age=0
And received

Accept-Ranges: bytes
Do you have an idea :confused: ?
Thank you for your help.


Cheers, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le vendredi 30 mars 2012 à 07:08, Adnan RIHAN a écrit :

On Fri, Mar 30, 2012 at 07:08:37AM +0200, Adnan RIHAN wrote:

Hi there,

What can I do T_T ?

Read How nginx processes a request

Look in your config file. Be aware of “include” directives. Look in
every server{} block. Look at “listen” and “server_name” directives in
each server{} block.

Identify which one server{} block is being used to handle this request.

Post the config for that server{}.

(If you can additionally identify which one location{} block within
that server{} block is being used for this request, then you could
limit what you post to: the server{} block outside of all location{}s,
plus the content of that one location{}. But that’s probably more work
than is necessary.)

If you can enable the debug log (will probably require rebuilding
nginx), then you will see lots of information about what nginx thinks
is happening.

But posting the relevant part of the config file is the best way to make
it easy for people help you.

f

Francis D. [email protected]

Ok.

Then, here is the vhost:

root /home/megasoun/public_html;

    proxy_redirect  http://www.mega-sound.be:8888 

http://www.mega-sound.be;

client_body_buffer_size 512k;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
include “/etc/cpnginx/custom/mega-sound.be”;
}

Here is the included file:

more_set_headers ‘X-My2Header: $request_uri’;
Here is the HTTP Headers for the root URL (www.mega-sound.be):

Set-Cookie: slrememberme=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT
X-MyMasterHeader: set
X-My2Header: /
Content-Encoding: gzip

Here is the headers for an MP3 (http://www.mega-sound.be/adata/362.mp3):

Cache-Control: max-age=86400
Accept-Ranges: bytes

Is it enough ?
Thank you for your help.

Cordialement, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le vendredi 30 mars 2012 à 10:36, Francis D. a écrit :

I knew that location are matched 1 time but I didn’t noticed the |mp3
T_T

Thank you very much, I’m understanding more and more nginx.
I think it’s done, I just have to deal with the CPanel NGinx module devs
to change include place (in order to avoid this case another time).

Good night !

Cordialement, Adnan RIHAN.
Président-Fondateur de l’association (de loi 1901) Virtual-Info
(http://www.virtual-info.info/), hébergeur Web et Serveurs de Jeux.
Consultant (http://rihan.fr/)-Technicien Supérieur en Informatique de
Gestion.
Ambassadeur Qt (YOURLS — Your Own URL Shortener | https://lyt.me/) (Projet Tag-PG
(http://rihan.fr/fr/projets/tagpg)).

Le vendredi 30 mars 2012 à 20:14, Francis D. a écrit :