Install Nginx

nginx -V #查看编译选项
2>&1 nginx -V | tr ' ' '\n'
nginx -t # 检查配置文件是否正确
nginx -s stop
nginx -s reload #平滑重启

Red Hat NGINX Init Script

https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

sed -i 's/nginx=".*"/nginx="\/usr\/local\/nginx\/sbin\/nginx"/' /etc/init.d/nginx
sed -i 's/NGINX_CONF_FILE=".*"/NGINX_CONF_FILE="\/usr\/local\/nginx\/conf\/nginx.conf"/g' /etc/init.d/nginx
chmod a+x /etc/init.d/nginx
service nginx start
service nginx stop
chkconfig nginx on

Install Nginx Lua

Install LuaJIT

Download NDK

cd /usr/local/src
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
mv v0.3.0.tar.gz ngx_devel_kit-0.3.0.tar.gz
tar -zxvf ngx_devel_kit-0.3.0.tar.gz
cd ngx_devel_kit-0.3.0
pwd

Download ngx_http_lua_module

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.11.tar.gz
mv v0.10.11.tar.gz lua-nginx-module-0.10.11.tar.gz
tar -zxvf lua-nginx-module-0.10.11
cd lua-nginx-module-0.10.11
pwd

Install

cd /usr/local/src/nginx-1.12.2
./configure --prefix=/usr/local/nginx \
 # nginx 编译参数
--with-ld-opt="-Wl,-rpath,/usr/local/luajit/lib" \
--add-module=/usr/local/src/ngx_devel_kit-0.3.0 \
--add-module=/usr/local/src/lua-nginx-module-0.10.11 \
&& make \
&& make install

ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

Test

nginx.conf

        location ~* ^/lua(/.*) {
                default_type 'text/plain';
                content_by_lua 'ngx.say("hello, lua")';
        }
/usr/local/nginx/sbin/nginx  -t
/usr/local/nginx/sbin/nginx -s reload
curl http://localhost/lua/

Nginx负载均衡

upstream myServer{
    ip_hash;
    server 42.156.140.7;
    server 222.187.254.227 weight=2;
    server 42.120.158.67;
    server 110.75.115.70;
}

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        proxy_pass http://myServer;
        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;