Systèmes d'exploitation : Ngnix : Gérer la mise en cache des fichiers static

Le 2/11/2011
Comments
Add One

Vous pouvez procéder de la manière suivante

location ~* \.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
    expires 24h;
    log_not_found off;
}

Nginx : Sécuriser vos Vhosts

Le 2/11/2011
Comments
Add One

Uniquement les requêtes GET HEAD et POST sont autorisé

if ($request_method !~ ^(GET|HEAD|POST)$ ) {
    return 444;
}

Pour ne pas loger les accès aux fichiers favicon.ico et robots.txt

location = /favicon.ico {
    log_not_found off;
    access_log off;
}
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

L’acces aux fichiers caché peuvent être dangereux tel qu’un .bash_history contant un mot de passe tapé par erreur ou un .my.cnf avec un compte MySQL, l’accès a tous les fichiers caché est donc interdit :

location ~ /\. {
    deny all;
    access_log off;
    log_not_found off;
}

Quelque limite pour éviter les Buffer Overflows

client_body_buffer_size  1K;
client_header_buffer_size 1k;
client_max_body_size 1m;
large_client_header_buffers 2 1k;

Dans le cas ou un petit malin essaye de vous faire un référencement douteux

if ( $http_referer ~* (babes|forsale|girl|jewelry|love|nudit|organic|poker|porn|sex|teen) )
{
    return 403;
}

Nginx : Utilisation de la compression gzip

Le 2/11/2011
Comments
Add One

Liste de paramètre possible pour activer la compression Gzip au niveau de “http”

gzip            on;
gzip_min_length 1000;
gzip_vary       on;
gzip_comp_level 6;
gzip_proxied    any;
gzip_types      text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers    16 8k;
gzip_disable    "MSIE [1-6].(?!.*SV1)";

Nginx : Reverse proxy transparents

Le 2/11/2011
Comments
Add One

Vous pouvez utiliser nginx pour cacher ces propres “server”.
Pour cela, l’idée est simple, vous avez un “server” qui ecoute sur votre IP public et vos autre “server” applicatif écoute sur une ip privée ou local

Dans l’exemple de configuration ci-dessous, les “servers” applicatif écoute sur 127.0.0.1:80

server {
    listen       111.222.111.2222:80 default;
    server_name  _;
    server_name_in_redirect off;

    location / {
        set $no_cache "";
        if ($request_method !~ ^(GET|HEAD)$) {
            set $no_cache "1";
        }
        if ($no_cache = "1") {
            add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
            add_header X-Microcachable "0";
        }
        if ($http_cookie ~* "_mcnc") {
            set $no_cache "1";
        }
        proxy_no_cache $no_cache;
        proxy_cache_bypass $no_cache;
        proxy_pass http://127.0.0.1;

        proxy_cache microcache;
        proxy_cache_key $scheme$host$request_method$request_uri;
        proxy_cache_valid 200 1s;
        proxy_cache_use_stale updating;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_max_temp_file_size 1M;
    }
    access_log  /var/log/nginx/access.log vhosts;
    error_log  /var/log/nginx/error.log info;
}

Vous devez ajouter dans votre “http” la ligne ci-dessous pour activer le cache Nginx

proxy_cache_path /var/spool/nginx/cache levels=1:2 keys_zone=votreclef:5m max_size=1000m;

Dans cette exemple, ce dossier de cache ne dépassera pas 1000Mo et sera purgé par Nginx
Vous pouvez supprimé la directive access_log pour utiliser l’astuce indiqué dans cet article

Nginx : Configurer vos logs

Le 2/11/2011
Comments
Add One

Ajouter la ligne ci-dessous dans vos “server”, la variable $host reprend le hostname demandé

access_log  /var/log/nginx/$host-access.log;

Pour configurer la rotation des logs sous FreeBSD, ajouter la ligne ci-dessous dans le fichier de configuration /etc/newsyslog.conf

/var/log/nginx/*.log www:root   640  7     *    @T00    GJ        /var/run/nginx.pid 30

Shell : SSH, les séquences d’échappement

Le 27/10/2011
Comments
Add One

Cela peut par exemple vous servir pour gérer ou retrouver vos tunnel.

Extrait du man :

 ESCAPE CHARACTERS
     When a pseudo-terminal has been requested, ssh supports a number of func-
     tions through the use of an escape character.

     A single tilde character can be sent as ~~ or by following the tilde by a
     character other than those described below.  The escape character must
     always follow a newline to be interpreted as special.  The escape charac-
     ter can be changed in configuration files using the EscapeChar configura-
     tion directive or on the command line by the -e option.

     The supported escapes (assuming the default `~') are:

     ~.      Disconnect.

     ~^Z     Background ssh.

     ~#      List forwarded connections.

     ~&      Background ssh at logout when waiting for forwarded connection /
             X11 sessions to terminate.

     ~?      Display a list of escape characters.

     ~B      Send a BREAK to the remote system (only useful for SSH protocol
             version 2 and if the peer supports it).

     ~C      Open command line.  Currently this allows the addition of port
             forwardings using the -L, -R and -D options (see above).  It also
             allows the cancellation of existing port-forwardings with
             -KL[bind_address:]port for local, -KR[bind_address:]port for re-
             mote and -KD[bind_address:]port for dynamic port-forwardings.
             !command allows the user to execute a local command if the
             PermitLocalCommand option is enabled in ssh_config(5).  Basic
             help is available, using the -h option.

     ~R      Request rekeying of the connection (only useful for SSH protocol
             version 2 and if the peer supports it).

MySQL : Les variables système

Le 26/10/2011
Comments
Add One

La liste des variables système MySQL se trouve sur http://dev.mysql.com[en].

Il y est indiqué si ces variables sont dynamique ou non, si elle le sont, vous pouvez les changer a chaud (ne pas oublier de mettre a jour le fichier de configuration MySQL ;) ).

Afficher un ou des variables :

mysql> show global variables like '%sort_buffer%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| myisam_sort_buffer_size | 16777216 |
| sort_buffer_size        | 524280   |
+-------------------------+----------+
2 rows in set (0.00 sec)

Modifier une variable :

mysql> set global sort_buffer_size = 16 * 1024 * 1024;
Query OK, 0 rows affected (0.00 sec)

Divers : Steve Jobs

Le 5/10/2011
Comments
Add One

STEVE sans toi nous serions encore en train de planter comme des cons sur Windows !

MySQL : Lister toutes les tables/base

Le 29/09/2011
Comments
Add One

Cette requête va lister toutes les bases/tables du serveur MySQL en ignorant les “bases” performance_schema et information_schema mais aussi en filtrant les tables dont le nom commence par tmp_ et la table test.

SELECT table_schema, table_name
	FROM information_schema.tables
	WHERE table_schema NOT IN ('performance_schema','information_schema')
		AND table_name NOT REGEXP '^tmp_|^test$'
	ORDER BY table_schema;

Vous pouvez filtrer sur tout ce qui est dans la table “tables”

mysql> desc tables;
+-----------------+---------------------+------+-----+---------+-------+
| Field           | Type                | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG   | varchar(512)        | YES  |     | NULL    |       |
| TABLE_SCHEMA    | varchar(64)         | NO   |     |         |       |
| TABLE_NAME      | varchar(64)         | NO   |     |         |       |
| TABLE_TYPE      | varchar(64)         | NO   |     |         |       |
| ENGINE          | varchar(64)         | YES  |     | NULL    |       |
| VERSION         | bigint(21) unsigned | YES  |     | NULL    |       |
| ROW_FORMAT      | varchar(10)         | YES  |     | NULL    |       |
| TABLE_ROWS      | bigint(21) unsigned | YES  |     | NULL    |       |
| AVG_ROW_LENGTH  | bigint(21) unsigned | YES  |     | NULL    |       |
| DATA_LENGTH     | bigint(21) unsigned | YES  |     | NULL    |       |
| MAX_DATA_LENGTH | bigint(21) unsigned | YES  |     | NULL    |       |
| INDEX_LENGTH    | bigint(21) unsigned | YES  |     | NULL    |       |
| DATA_FREE       | bigint(21) unsigned | YES  |     | NULL    |       |
| AUTO_INCREMENT  | bigint(21) unsigned | YES  |     | NULL    |       |
| CREATE_TIME     | datetime            | YES  |     | NULL    |       |
| UPDATE_TIME     | datetime            | YES  |     | NULL    |       |
| CHECK_TIME      | datetime            | YES  |     | NULL    |       |
| TABLE_COLLATION | varchar(32)         | YES  |     | NULL    |       |
| CHECKSUM        | bigint(21) unsigned | YES  |     | NULL    |       |
| CREATE_OPTIONS  | varchar(255)        | YES  |     | NULL    |       |
| TABLE_COMMENT   | varchar(80)         | NO   |     |         |       |
+-----------------+---------------------+------+-----+---------+-------+

MySQL : Lister les tables en InnoDB/MyISAM

Le 29/09/2011
Comments
Add One
SELECT table_schema, table_name, engine
FROM information_schema.tables
WHERE table_schema NOT IN ('performance_schema','information_schema')
AND engine != 'NULL'
ORDER BY table_schema;