Category Archives: nginx

CentOS7.0にnginx1.7.4をインストールするメモ

1.g++、gcc、openssl-devel、pcre-develとzlib-develのインストール
$ yum install gcc-c++
$ yum install pcre pcre-devel
$ yum install zlib zlib-devel
$ yum install openssl openssl–devel

2.nginxのインストール
$ find -name nginx
$ yum remove nginx
$ cd /usr/local

nginxのダウンロード
$ wget http://nginx.org/download/nginx-1.7.4.tar.gz

nginxを解凍
$ tar -zxvf nginx-1.7.4.tar.gz
$ cd nginx-1.7.4

$ ./configure $ディフォルトインストール先/usr/local/nginx
$ make
$ make install

nginxの確認
$ whereis nginx

「nginx入門」httpsを設定する方法メモ

1.nginxのSSLモジュールをインストール
#./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –with-http_realip_module
#make
#make install

2.SSLの設定
upstream tomcats {
server 127.0.0.1:8080 weight=10;
}

server {
listen 443;
server_name www.xxx.com;

ssl on;

ssl_certificate /usr/local/cert/xxx.pem;
ssl_certificate_key /usr/local/cert/xxx.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

location / {
root html;
proxy_pass http://tomcats;
index index.html index.htm;
}

}

3.httpsリダイレクト
rewrite ^(.*)$ https://$host$1 permanent;

location / {
root html;
proxy_pass http://tomcats;
index index.html index.htm;
}

4.nginx再起動
/usr/local/nginx/sbin/nginx -s reload

nginxでSSL/HTTPS設定をするメモ

サンプルコード:
server {
listen   80;
listen 443 ssl spdy;
server_name www.domain.com;
ssl on;
ssl_certificate sample.net.crt;
ssl_certificate_key sample.net.key;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout  10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE:!kEDH;
ssl_prefer_server_ciphers on;

ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/nginx/startssl_trust_chain.crt;
add_header Strict-Transport-Security “max-age=31536000”;
resolver 8.8.8.8 8.8.4.4;
location / {
root   html;
index  index.html index.htm;
}
}

centosにphp-fpm起動スクリプトを設定する方法

#vim etc/init.d/php-fpm

#!/bin/bash
#
# Startup script for the PHP-FPM server.
#
# chkconfig: 345 85 15
# description: PHP is an HTML-embedded scripting language
# processname: php-fpm
# config: /usr/local/php/etc/php.ini

# Source function library.
. /etc/rc.d/init.d/functions

PHP_PATH=/usr/local
DESC=”php-fpm daemon”
NAME=php-fpm
DAEMON=$PHP_PATH/php/sbin/$NAME
CONFIGFILE=$PHP_PATH/php/etc/php-fpm.conf
PIDFILE=$PHP_PATH/php/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

rh_start() {
$DAEMON -y $CONFIGFILE || echo -n ” already running”
}

rh_stop() {
kill -QUIT `cat $PIDFILE` || echo -n ” not running”
}

rh_reload() {
kill -HUP `cat $PIDFILE` || echo -n ” can’t reload”
}

case “$1” in
start)
echo -n “Starting $DESC: $NAME”
rh_start
echo “.”
;;
stop)
echo -n “Stopping $DESC: $NAME”
rh_stop
echo “.”
;;
reload)
echo -n “Reloading $DESC configuration…”
rh_reload
echo “reloaded.”
;;
restart)
echo -n “Restarting $DESC: $NAME”
rh_stop
sleep 1
rh_start
echo “.”
;;
*)
echo “Usage: $SCRIPTNAME {start|stop|restart|reload}” >&2
exit 3
;;
esac
exit 0

#sudo chmod +x /etc/init.d/php-fpm
#sudo /sbin/chkconfig php-fpm on
#sudo /sbin/chkconfig –list php-fpm
php-fpm           0:off   1:off   2:on    3:on    4:on    5:on    6:off

php-fpm起動、停止、再起動
service php-fpm start
service php-fpm stop
service php-fpm restart
service php-fpm reload

/etc/init.d/php-fpm start
/etc/init.d/php-fpm stop
/etc/init.d/php-fpm restart
/etc/init.d/php-fpm reload

Linuxにnginx起動スクリプトを設定するコード

サンプルコード

#!/bin/bash
#
# Startup script for Nginx – this script starts and stops the nginx daemon
#
# chkconfig:   – 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ “$NETWORKING” = “no” ] && exit 0

nginx=”/usr/local/nginx/sbin/nginx”
prog=$(basename $nginx)

NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf”

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $”Starting $prog: ”
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $”Stopping $prog: ”
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $”Reloading $prog: ”
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case “$1″ in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
exit 2
esac

#sudo chmod +x /etc/init.d/nginx
#sudo /sbin/chkconfig nginx on
#sudo /sbin/chkconfig –list nginx
nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off

nginx起動、停止、再起動
#service nginx start
#service nginx stop
#service nginx restart
#service nginx reload

/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart
/etc/init.d/nginx reload

「nginxサーバ」.htaccessファイルを設定するメモ

1.ファイル.htaccessの作成
# nginx rewrite rule
rewrite ^(.*?)/article/.*?-(d+)-(d+).html$  $1/display.html?id=$2&page=$3   break;
rewrite ^(.*?)/category/.*?-(d+)-(d+).html$ $1/listdata.html?id=$2&page=$3   break;
rewrite ^(.*?)/tag/([^/]+)/?$  $1/tag.html?w=$2    break;
rewrite ^(.*?)/tag/([^/]+)/(d+)$    $1/tag.html?w=$2&page=$3    break;
# end nginx rewrite rule

2.nginx.confの修正
# vim  /usr/local/etc/nginx/nginx.conf
下記追加
include /usr/www/.htaccess
nginx再起動
# /usr/local/etc/rc.d/nginx restart

nginxサーバの設定についてまとめ

サンプルコード
if (!-e $request_filename) {
proxy_pass http://sample.com;
}

if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}

location ~* \.(gif|jpg|png|swf|flv|swf)$ {
valid_referers none blocked www.sample.com sample.com;
if ( $invalid_referer ) {
rewrite ^/ http://$host/logo.png;
}
}

//有効期間
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if ( -f $request_filename ) {
expires 1h;
break;
}
}

//ディレクトリアクセス禁止
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}

「nginx」リダイレクトrewrite設定のメモ

設定内容
server {
listen 80;
server_name sample.com www.sample.com;
root /home/web;
if($host !~ “www\.sample\.com$”) {
rewrite ^(.*) http://www.sample.com$1 permanent;
}
}
nginx再起動
/etc/init.d/nginx restart

nginxで複数ポート(9000、9001、9002、9003)を起動する方法

1.複数ポート
ポート番号9000 9001 9002 9003

2.システム環境
CentOS 6.5 64ビット
nginx1.9.11

3.nginx.conf の設定
stream {
upstream 9000 {
hash $remote_addr consistent;
server 172.17.2.101:9000 max_fails=3 fail_timeout=30s;
}
upstream 9001 {
hash $remote_addr consistent;
server 172.17.2.101:9001 max_fails=3 fail_timeout=30s;
}
upstream 9002 {
hash $remote_addr consistent;
server 172.17.2.101:9002 max_fails=3 fail_timeout=30s;
}
upstream 9003 {
hash $remote_addr consistent;
server 172.17.2.101:9003 max_fails=3 fail_timeout=30s;
}
server {
listen 9000;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass 9000;
}

server {
listen 9001;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass 9001;
}
server {
listen 9002;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass 9002;
}
server {
listen 9003;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass 9003;
}
}
4.nginx再起動
#pkill -9 nginx
# /usr/local/nginx/sbin/nginx

CentOSにnginx1.7.3をイソースコードからンストールする手順

1.依存パッケージのインストール
# yum install -y pcre pcre-devel
# yum install -y zlib zlib-devel
# yum install -y openssl openssl-devel

2.nginxのダウンロード、インストール
# mkdir nginx-src && cd nginx-src
# wget http://nginx.org/download/nginx-1.7.3.tar.gz
# tar xzf nginx-1.7.3.tar.gz
# cd nginx-1.7.3
# ./configure
# make
# make install
# whereis nginx
nginx: /usr/local/nginx

バージョンの確認
# ./nginx -h
nginx version: nginx/1.7.3
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file

3.システムサービスに追加
# vi /etc/init.d/nginx

下記コードを追記
#!/bin/sh
# chkconfig: 2345 85 15
# Startup script for the nginx Web Server
# description: nginx is a World Wide Web server.
# It is used to serve HTML files and CGI.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC=”nginx deamon”
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME

test -x $DAEMON || exit 0

d_start(){
$DAEMON || echo -n “already running”
}

d_stop(){
$DAEMON -s quit || echo -n “not running”
}

d_reload(){
$DAEMON -s reload || echo -n “can not reload”
}

case “$1” in
start)
echo -n “Starting $DESC: $NAME”
d_start
echo “.”
;;
stop)
echo -n “Stopping $DESC: $NAME”
d_stop
echo “.”
;;
reload)
echo -n “Reloading $DESC conf…”
d_reload
echo “reload .”
;;
restart)
echo -n “Restarting $DESC: $NAME”
d_stop
sleep 2
d_start
echo “.”
;;
*)
echo “Usage: $ScRIPTNAME {start|stop|reload|restart}” >&2
exit 3
;;
esac

exit 0

4.nginxを起動、自動起動の設定
# chmod +x /etc/init.d/nginx
# chkconfig –add nginx
# chkconfig nginx on/off
# chkconfig –list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

1 / 512345