nginx autoindex开启目录浏览和索引

Nginx默认是不允许列出整个目录的。如需此功能,打开nginx.conf文件或你要启用目录浏览虚拟主机的配置文件,在server或location 段里添加上autoindex on;来启用目录流量,下面会分情况进行说明。

另外Nginx的目录流量有两个比较有用的参数,可以根据自己的需求添加:

autoindex_exact_size off;
默认为on,显示出文件的确切大小,单位是bytes。
改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
改为on后,显示的文件时间为文件的服务器时间

1、整个虚拟主机开启目录流量

在server段添加

location / {
    autoindex on;
    autoindex_localtime on; #之类的参数写这里
}

2、单独目录开启目录流量

2.1:直接二级目录开启目录流量

location /down/ {
    autoindex on;
}

2.2:虚拟目录开启目录流量

location /down/ {
    alias /home/wwwroot/lnmp/test/;
    autoindex on;
}

详细参照:http://nginx.org/en/docs/http/ngx_http_autoindex_module.html

需要注意root和alias的区别:

alias 设置的目录是准确的,可以理解为linux的 ln命令创建软连接,location就是软连接的名字。如上面2.2例子访问 http://域名/down/vpser.txt 是直接访问的/home/wwwroot/lnmp/test/下面的vpser.txt文件。

root 设置的目录是根目录,locatoin里所指定名称的目录,必须在root设定下的目录有相同名字的目录。如果将上面2.2例子里的alias改成root 访问 http://域名/down/vpser.txt 是直接访问的的/home/wwwroot/lnmp/test/down/ 目录下的vpser.txt文件。

需要注意的是alias目录必须要以 / 结尾且alias只能在location中使用。

如果想希望做出漂亮的目录列表,支持header,footer则可以安装三方插件:
http://wiki.nginx.org/NginxNgxFancyIndex

重启nginx,使其生效。

nginx auth_basic为目录添加密码保护

nginx可以为网站或目录甚至特定的文件设置密码认证。密码必须是crypt加密的。可以用apache的htpasswd来创建密码。

格式为:htpasswd -b -c site_pass username password

site_pass为密码文件。放在同nginx配置文件同一目录下,当然你也可以放在其它目录下,那在nginx的配置文件中就要写明绝对地址或相对当前目录的地址。

如果你输入htpasswd命令提示没有找到命令时,你需要安装httpd.如centos是yum install httpd

如果是为了给网站加上认证,可以直接将认证语句写在nginx的配置server段中。

如果是为了给目录加上认证,就需要写成目录形式了。同时,还要在目录中加上php的执行,否则php就会被下载而不执行了。

例如:基于整个网站的认证,auth_basic在php解释之前。

server {
    listen       80;
    server_name www.wkii.org akii.org;
    root  /www/akii;
    index index.html index.htm index.php;

    auth_basic "input you user name and  password";
    auth_basic_user_file /usr/local/nginx/conf/vhost/nginx_passwd;

    location ~ .php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    location ~ /.ht {
         deny  all;
    }
    access_log /logs/akii.org_access.log main;
}

针对目录的认证,在一个单独的location中,并且在该location中嵌套一个解释php的location,否则php文件不会执行并且会被下载。auth_basic在嵌套的location之后。

server {
    listen       80;
    server_name www.wkii.org akii.org;
    root  /www/akii;
    index index.html index.htm index.php;

    location ~ ^/admin/.* {
        location ~ .php$ {
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
        }

        auth_basic "auth";
        auth_basic_user_file /usr/local/nginx/conf/vhost/auth/admin.pass;
    }

    location ~ .php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /.ht {
         deny  all;
    }
    access_log /logs/akii.org_access.log main;
}

这里有一个细节,就是location ~ ^/admin/.* {…} 保护admin目录下的所有文件。如果你只设了/admin/ 那么直接输入/admin/index.php还是可以访问并且运行的。 ^/admin/.* 意为保护该目录下所有文件。当然,只需要一次认证。并不会每次请求或每请求一个文件都要认证一下。

nginx指令add_before_body add_after_body向响应体前或后添加内容

ngx_http_addition_module在响应之前或者之后追加文本内容,比如想在站点底部追加一个js或者css,可以使用这个模块来实现,这个模块和淘宝开发的nginx footer模块有点类似,但是还是有不同. 这个模块需要依赖子请求,nginx footer依赖nginx写死的配置.

1. 安装nginx

# wget http://nginx.org/download/nginx-1.4.2.tar.gz
# tar -xzvf nginx-1.4.2.tar.gz
# cd nginx-1.4.2
#  --prefix=/usr/local/nginx-1.4.2 --with-http_stub_status_module --with-http_addition_module
# make
# make install

2. 指令(Directives)

语法: add_before_body uri;
默认值: —
配置段: http, server, location
发起一个子请求,请求给定的uri,并且将内容追加到主题响应的内容之前。

语法: add_after_body uri;
默认值: —
配置段: http, server, location
发起一个子请求,请求给定的uri,并且将内容追加到主题响应的内容之后。

syntax: addition_types mime-type …;
default: addition_types text/html;
context: http, server, location
这个指令在0.7.9开始支持,指定需要被追加内容的MIME类型,默认为“text/html”,如果制定为*,那么所有的

3. nginx配置addition

3.1 配置nginx.conf

server {
    listen       80;
    server_name  www.ttlsa.com;

    root /data/site/www.ttlsa.com;    

    location / {
        add_before_body /2013/10/header.html;
        add_after_body  /2013/10/footer.html;
    }
}

3.2 测试

以下三个文件,对应请求的主体文件和add_before_body、add_after_body对应的内容

# cat /data/site/test.ttlsa.com/2013/10/20131001_add.html 
<html>
<head>
<title>I am title</title>
</head>
<body>
ngx_http_addition_module
</body>
</html>

# cat /data/site/test.ttlsa.com/2013/10/header.html 
I am header!

# cat /data/site/test.ttlsa.com/2013/10/footer.html 
footer - ttlsa

看到20131001_add.html的顶部和底部分别嵌入了子请求header.html和footer的内容。

# curl test.ttlsa.com/2013/10/20131001_add.html           
I am header!
<html>
<head>
<title>I am title</title>
</head>
<body>
ngx_http_addition_module
</body>
</html>
footer - ttlsa

4. 结束语

addition模块与上节上节nginx sub替换响应内容模块应用场景有点相同,具体怎么使用,大家结合实际情况来使用.

使用Nginx的error_page指令自定义404 50x错误页面

当nginx出现404和50x错误时,直接显示默认的nginx出错页面显得非常地不友好,我们可以自定义错误页面为用户显示友好的错误页面。这个用到了nginx的error_page指令。下面介绍在nginx配置自定义的404 50x错误页面。

创建你的自定义错误页面

我们把错误页面放到/usr/share/nginx/html目录,这个目录一般是在ubuntu通过apt安装时默认的nginx网站根目录。我们命名404错误页面为custom_404.html,500级别的错误页面称为custom_50x.html。执行如下命令创建错误页面:

echo "<h1 style='color:red'>Error 404: Not found :-(</h1>" | sudo tee /usr/share/nginx/html/custom_404.html
echo "<p>I have no idea where that file is, sorry.  Are you sure you typed in the correct URL?</p>" | sudo tee -a /usr/share/nginx/html/custom_404.html
echo "<h1>Oops! Something went wrong...</h1>" | sudo tee /usr/share/nginx/html/custom_50x.html
echo "<p>We seem to be having some technical difficulties. Hang tight.</p>" | sudo tee -a /usr/share/nginx/html/custom_50x.html

配置nginx使用你的错误页面

我们这里假设默认的nginx server配置在/etc/nginx/sites-enabled/default,打开此文件

vi /etc/nginx/sites-enabled/default

配置404错误页面

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        . . .

        error_page 404 /custom_404.html;
        location = /custom_404.html {
                root /usr/share/nginx/html;
                internal;
        }
}

配置500级别错误页面

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        . . .

        error_page 404 /custom_404.html;
        location = /custom_404.html {
                root /usr/share/nginx/html;
                internal;
        }

        error_page 500 502 503 504 /custom_50x.html;
        location = /custom_50x.html {
                root /usr/share/nginx/html;
                internal;
        }

        location /testing {
                fastcgi_pass unix:/does/not/exist;
        }
}

重启nginx并测试

检查配置文件语法

sudo nginx -t

重启nginx

sudo service nginx restart

测试404,在浏览器输入http://server_domain_or_IP/thiswillerror,将显示
Nginx
测试500,在浏览器输入http://server_domain_or_IP/testing,将显示
Nginx

使用Apache的deny allow禁止和允许指定IP的访问

Apache提供了allow和deny指令允许我们禁止指定访客或允许指定访客访问网站。这有助于我们屏蔽一些恶意访问,或只允许网站管理员访问特定的网站区域,如管理后台。

要配置限制和拒绝指定IP访问,在网站目录下创建.htaccess,添加如下内容:

order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all

上面表示拒绝来自255.0.0.0和123.45.6.的IP的访客访问。第二个IP没有第4位,说明匹配IP时只需要匹配前三位就行。如123.45.6.10’和’123.45.6.255这两个IP都将禁止访问

要配置除了你自己之外禁止所有IP访问,在网站目录下创建.htaccess,并添加如下内容:

order allow,deny
allow from 255.0.0.0
deny from all

上面的配置说明只允许IP 255.0.0.0访问,其它的IP禁止访问。
被禁止的访问将显示”403 Forbbiden”错误页面。可以自定义此错误页面

CentOS7 yum安装Nginx Web服务器

关于Nginx

Nginx是一个高性能的web服务器软件。它与Apache HTTP Server比较,更加地灵活和轻量级。

本文介绍如何在CentOS7上使用yum安装Nginx。

步骤1 – 添加Nginx仓库

执行如下命令配置Nginx官方仓库

cat > /etc/yum.repos.d/nginx.repo << EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
EOF

步骤2 – 安装Nginx

现在使用yum安装Nginx:

sudo yum -y install nginx

步骤3 – 启动nginx

Nginx安装后不会自动启动,执行如下命令启动nginx

sudo systemctl start nginx

如何启用了firewall,执行如下命令来允许HTTP和HTTPS流量:

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

这样你就可以在浏览器输入http://server_domain_name_or_IP/来访问nginx了,如图
Nginx

步骤4 – 设置开机启动nginx

sudo systemctl enable nginx

Nginx使用limit_rate limit_conn限制文件下载速度

今天搜索如何在一个Nginx服务器限制下载速度,该服务器每秒有大量的mp4视频文件请求。遇到的问题是,每个用户发起大量的请求消耗了大量的服务器带宽,所以使用Nginx的limit_rate和limit_rate_after这两个指令解决此问题。

基于我自己服务器的配置,我在一个location块使用这两个指令:

location ^~ /videos/ {
    ...
    limit_rate_after 1m;
    limit_rate 150k;
    ...
}

第一个指令limit_rate_after,从下载到你指定的文件大小之后开始限速,然后第二个指令limit_rate,设置最高下载速度。

要注意的是上面的设置是限制的是每一个连接的下载速度,所以如果一个用户打开了多个连接下载,那么它的下载速度就能达到单个连接的限速乘以连接数。不过我们可以使用limit_zone和limit_conn这两个指令限制其连接数。例如:
在server块配置中:

limit_rate 128K; limit_zone one $binary_remote_addr 10m;

在location配置块中:

limit_conn one 10;

上面的配置表示每个连接允许的速率为1Mbit,最大连接数10个。

CentOS7源码编译安装Nginx

Web服务器Nginx

LNMP是一组众所周知的Web网站服务器架构环境,即由Linux+Nginx+MySQL+PHP(MySQL有时也指 Mariadb)组合成一个高性能、轻量、稳定、扩展性强的Web网站服务器架构环境。

Nginx (“engine x”) 作为Web服务器软件,是一个轻量级、高性能的HTTP和反向代理服务器,负 载均衡服务器,及电子邮件IMAP/POP3/SMTP 服务器。Nginx性能稳定、功能丰富、运维简单、效率高 、并发能力强、处理静态文件速度快且消耗系统资源极少。

Nginx的版本

Nginx版本分为主线版和稳定版,主线版更新速度较快,从官网上看大约一个月更新1-2次,目前 最新主线版已更新到nginx-1.9.10,而官方宣布的最新稳定版则是nginx-1.8.1,and本文就以1.8.1 版为例演示其在CentOS7上的安装和配置过程。Nginx官方网站http://nginx.org/。

Nginx的依赖程序

  • 1、zlib:用于支持gzip模块
  • 2、pcre:用于支持rewrite模块
  • 3、openssl:用于支持ssl功能
  • 使用yum安装zlib、pcre、openssl软件包

    [root@www ~]# yum install zlib pcre pcre-devel openssl openssl-devel
    

    Nginx-1.8.1的安装

    step1:创建nginx用户

    创建一个nginx的运行用户

    [root@www ~]# useradd -s /sbin/nologin nginx
    [root@www ~]# id nginx
    uid=1000(nginx) gid=1001(nginx) groups=1001(nginx)
    

    step2:Nginx编译参数

    –user 指定启动程序所属用户
    –group 指定组
    –prefix 指定安装路径
    –sbin-path 设置nginx二进制文件的路径名
    –conf-path 指定配置文件路径
    –error-log-path 错误日志文件路径
    –http-log-path 指定访问日志文件路径
    –http-client-body-temp-path 设置存储HTTP客户端请求主体的临时文件路径
    –http-proxy-temp-path 设置存储HTTP代理临时文件的路径
    –http-fastcgi-temp-path 设置存储HTTP fastcgi的临时文件的路径
    –pid-path 设置nginx.pid文件路径
    –lock-path 设置nginx.lock文件路径
    –with-openssl 启用SSL
    –with-pcre 启用正则表达式
    –with-http_stub_status_module 安装可以监控nginx状态的模块
    –with-http_ssl_module 启用SSL支持
    –with-http_gzip_static_module 启用gzip压缩


    [root@www nginx-1.8.1]# ./configure --user=nginx --group=nginx --prefix=/opt/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-http_sub_module

    Nginx


    [root@www nginx-1.8.1]# make [root@www nginx-1.8.1]# make install

    make安装完成使用nginx -V 查看版本和编译参数


    [root@www nginx-1.8.1]# nginx -V nginx version: nginx/1.8.1 built by gcc 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --user=nginx --group=nginx --prefix=/opt/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-http_sub_module

    查看ngin进程和端口号


    [root@www ~]# netstat -ntlp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4415/nginx: master

    step3:控制nginx服务的命令

    1、启动:nginx
    2、停止:nginx -s stop
    3、退出:nginx -s quit
    4、重启:nginx -s reopen
    5、重新加载:nginx -s reload
    6、平滑启动:kill -HUP pid(kill -HUP cat /var/run/nginx.pid

    step4:创建nginx启动脚本


    #!/bin/bash # chkconfig: - 18 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/sbin/nginx" NGINX_CONF="/etc/nginx/nginx.conf" NGINX_PID="/var/run/nginx.pid" RETVAL=0 prog="Nginx" #Source networking configuration . /etc/sysconfig/network # Check networking is up [ ${NETWORKING} = "no" ] && exit 0 [ -x $NGINX_SBIN ] || exit 0 start() { echo -n $"Starting $prog: " touch /var/lock/subsys/nginx daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /var/lock/subsys/nginx /var/run/nginx.pid RETVAL=$? echo return $RETVAL } reload(){ echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart(){ stop start } configtest(){ $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL

    设置开机启动


    [root@www ~]# chmod 755 /etc/init.d/nginx [root@www ~]# chkconfig --add nginx [root@www ~]# chkconfig nginx on [root@www ~]# service nginx stop Stopping nginx (via systemctl): [ OK ] [root@www ~]# service nginx start Starting nginx (via systemctl): [ OK ]

    设置防火墙规则,允许外部访问80端口


    [root@www ~]# firewall-cmd --permanent --add-port=80/tcp [root@www ~]# firewall-cmd --reload

    step5:测试访问

    在浏览器输入http://Your-IP/
    Nginx

    Ubuntu 16.04配置Nginx使用GeoIP

    本文解释如何在Ubuntu 16.04上使用nginx的GeoIP模块,以了解您的访问者来自哪里。 GeoIP模块设置多个变量,如$geoip_country_name,$geoip_country_code,$geoip_city等,您可以在PHP脚本中或直接在nginx配置中使用这些变量,例如,根据用户所在的国家/地区以不同的语言提供内容。

    了解Nginx是否支持GeoIP

    在我们开始之前,我们必须找出GeoIP模块是否内置到我们的nginx服务器:

    1. nginx -V

    root@server1:~# nginx -V
    nginx version: nginx/1.10.0 (Ubuntu)
    built with OpenSSL 1.0.2g-fips 1 Mar 2016
    TLS SNI support enabled
    configure arguments: –with-cc-opt=’-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2′ –with-ld-opt=’-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now’ –prefix=/usr/share/nginx –conf-path=/etc/nginx/nginx.conf –http-log-path=/var/log/nginx/access.log –error-log-path=/var/log/nginx/error.log –lock-path=/var/lock/nginx.lock –pid-path=/run/nginx.pid –http-client-body-temp-path=/var/lib/nginx/body –http-fastcgi-temp-path=/var/lib/nginx/fastcgi –http-proxy-temp-path=/var/lib/nginx/proxy –http-scgi-temp-path=/var/lib/nginx/scgi –http-uwsgi-temp-path=/var/lib/nginx/uwsgi –with-debug –with-pcre-jit –with-ipv6 –with-http_ssl_module –with-http_stub_status_module –with-http_realip_module –with-http_auth_request_module –with-http_addition_module –with-http_dav_module –with-http_geoip_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_image_filter_module –with-http_v2_module –with-http_sub_module –with-http_xslt_module –with-stream –with-stream_ssl_module –with-mail –with-mail_ssl_module –with-threads
    如果看到–with-http_geoip_module,则已经把geoip模块编译到nginx中了。

    下载GeoIP数据库

    在Debian和Ubuntu上,有一个通过apt安装的geoip数据库,但它有点过旧了,只包含GeoIP.dat(国家数据库),而不是GeoLiteCity.dat(城市数据库)。 因此,我们不安装该软件包,从GeoIP网站下载新的到/etc/nginx/geoip目录:

    1. mkdir /etc/nginx/geoip
    1. cd /etc/nginx/geoip
    2. wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
    3. gunzip GeoIP.dat.gz
    4. wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
    5. gunzip GeoLiteCity.dat.gz

    配置Nginx

    现在我们配置nginx。 打开/etc/nginx/nginx.conf …

    1. nano /etc/nginx/nginx.conf

    …并将geoip_country和geoip_city指令添加到http {}代码块中:

    1. […]
    2. http {
    3.  
    4.  ##
    5.  # Basic Settings
    6.  ##
    7.  
    8.  geoip_country /etc/nginx/geoip/GeoIP.dat; # the country IP database
    9.  geoip_city /etc/nginx/geoip/GeoLiteCity.dat; # the city IP database
    10. […]

    geoip_country指令可用变量如下:
    $geoip_country_code – 两个字母的国家/地区代码,例如RU,US。
    $geoip_country_code3 – 三个字母的国家/地区代码,例如,RUS,USA。
    $geoip_country_name – 国家/地区的(详细)名称,例如俄罗斯联邦,美国等。
    geoip_city指令提供以下变量:
    $geoip_city_country_code – 两个字母的国家/地区代码,例如RU,US。
    $geoip_city_country_code3 – 三字母国家/地区代码,例如,RUS,USA。
    $geoip_city_country_name – 国家/地区的名称,例如,俄罗斯联邦,美国 – 如果可用。
    $geoip_region – 区域的名称(省,地区,州,省,联邦土地等),例如,Moscow City,DC – 如果可用。
    $geoip_city – 城市的名称,例如,莫斯科,华盛顿,里斯本等 – 如果可用。
    $geoip_postal_code – 邮政编码或邮政编码(如果有)。
    $geoip_city_continent_code – 如果可用。
    $geoip_latitude – latitude – 如果可用。
    $geoip_longitude – longitude – 如果可用。
    为了使这些变量可用于您的PHP脚本,我们必须设置一些fastcgi_param指令。 最好在文件/etc/nginx/fastcgi_params中执行此操作,其他fastcgi_param指令为:

    1. nano /etc/nginx/fastcgi_params
    1. […]
    2. ### SET GEOIP Variables ###
    3. fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
    4. fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
    5. fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
    6.  
    7. fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
    8. fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
    9. fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
    10. fastcgi_param GEOIP_REGION $geoip_region;
    11. fastcgi_param GEOIP_CITY $geoip_city;
    12. fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
    13. fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
    14. fastcgi_param GEOIP_LATITUDE $geoip_latitude;
    15. fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

    (确保在你的location〜 .php $ {}容器包含/etc/nginx/fastcgi_params;。)
    如果你使用nginx作为反向代理,并希望将GeoIP变量传递给后端,你应该创建/编辑文件/etc/nginx/proxy.conf …

    1. nano /etc/nginx/proxy.conf

    …并添加以下行:

    1. […]
    2. ### SET GEOIP Variables ###
    3. proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
    4. proxy_set_header GEOIP_COUNTRY_CODE3 $geoip_country_code3;
    5. proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;
    6.  
    7. proxy_set_header GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
    8. proxy_set_header GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
    9. proxy_set_header GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
    10. proxy_set_header GEOIP_REGION $geoip_region;
    11. proxy_set_header GEOIP_CITY $geoip_city;
    12. proxy_set_header GEOIP_POSTAL_CODE $geoip_postal_code;
    13. proxy_set_header GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
    14. proxy_set_header GEOIP_LATITUDE $geoip_latitude;
    15. proxy_set_header GEOIP_LONGITUDE $geoip_longitude;

    (确保您在nginx代理配置中使用了include /etc/nginx/proxy.conf行,否则后端不能使用GeoIP变量。)
    现在重新加载nginx …

    1. systemctl reload nginx.service

    重新启动PHP-FPM,如下所示:

    1. systemctl restart php7.0-fpm.service

    简单的测试

    要查看GeoIP模块是否正常工作,我们可以在www.example.com根目录(例如/var/www/www.example.com/web)中创建一个PHP文件:

    1. nano /var/www/www.example.com/web/geoiptest.php

    我们可以按如下方式访问GeoIP变量:
    $geoip_country_code = getenv(GEOIP_COUNTRY_CODE);
    或者:
    $geoip_country_code = $_SERVER[‘GEOIP_COUNTRY_CODE’];

    1. <html>
    2. <body>
    3. <?php
    4.  
    5. $geoip_country_code = getenv(GEOIP_COUNTRY_CODE);
    6. /*
    7. $geoip_country_code = $_SERVER[‘GEOIP_COUNTRY_CODE’]; // works as well
    8. */
    9. $geoip_country_code3 = getenv(GEOIP_COUNTRY_CODE3);
    10. $geoip_country_name = getenv(GEOIP_COUNTRY_NAME);
    11.  
    12. $geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE);
    13. $geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3);
    14. $geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME);
    15. $geoip_region = getenv(GEOIP_REGION);
    16. $geoip_city = getenv(GEOIP_CITY);
    17. $geoip_postal_code = getenv(GEOIP_POSTAL_CODE);
    18. $geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE);
    19. $geoip_latitude = getenv(GEOIP_LATITUDE);
    20. $geoip_longitude = getenv(GEOIP_LONGITUDE);
    21.  
    22. echo ‘country_code: ‘.$geoip_country_code.'<br>’;
    23. echo ‘country_code3: ‘.$geoip_country_code3.'<br>’;
    24. echo ‘country_name: ‘.$geoip_country_name.'<br>’;
    25.  
    26. echo ‘city_country_code: ‘.$geoip_city_country_code.'<br>’;
    27. echo ‘city_country_code3: ‘.$geoip_city_country_code3.'<br>’;
    28. echo ‘city_country_name: ‘.$geoip_city_country_name.'<br>’;
    29. echo ‘region: ‘.$geoip_region.'<br>’;
    30. echo ‘city: ‘.$geoip_city.'<br>’;
    31. echo ‘postal_code: ‘.$geoip_postal_code.'<br>’;
    32. echo ‘city_continent_code: ‘.$geoip_city_continent_code.'<br>’;
    33. echo ‘latitude: ‘.$geoip_latitude.'<br>’;
    34. echo ‘longitude: ‘.$geoip_longitude.'<br>’;
    35.  
    36. ?>
    37. </body>
    38. </html>

    在浏览器打开此文件后将会看到你本地IP的详细信息

    docker化你的PHP应用环境Nginx PHP-FPM

    开始之前

    在开始之前,我们假设你对Docker使用已经有了一些经验。本文尝试使用Docker官方存储库来运行一个简单的PHP应用程序,环境是Nginx和PHP。首先第一件事当然是安装Docker(如果你还没安装)。第二获取Docker Compose。

    配置Nginx

    我们从配置Web服务器开始,这里我们使用Docker官方的Nginx镜像。既然我们准备使用Docker Compose,我们接下来先创建docker-compose.yml文件,其实docker nginx的80端口映射到宿主机的8080端口:

    1. web:
    2.  image: nginx:latest
    3.  ports:
    4.  – "8080:80"

    现在我们运行:

    1. docker-compose up

    现在我们应该通过ip:8080会得到nginx的默认页面了。
    Nginx
    现在nginx是启动了,让我们再来添加一些配置。首先更新docker-compose.yml使它挂载到一个本地目录。我们使用目录code。

    1. web:
    2.     image: nginx:latest
    3.     ports:
    4.         – "8080:80"
    5.     volumes:
    6.         – ./code:/code

    下一步我们创建一个虚拟机配置文件site.conf,设置其根目录为/code,把该文件放置到与docker-compose.yml同级目录。

    1. server {
    2.     index index.html;
    3.     server_name php-docker.local;
    4.     error_log  /var/log/nginx/error.log;
    5.     access_log /var/log/nginx/access.log;
    6.     root /code;
    7. }

    为了让这个虚拟机配置生效,我们还需要再次更改docker-compose.yml文件:

    1. web:
    2.     image: nginx:latest
    3.     ports:
    4.         – "8080:80"
    5.     volumes:
    6.         – ./code:/code
    7.         – ./site.conf:/etc/nginx/conf.d/site.conf

    此次更新是把本地的site.conf挂载docker nginx里的/etc/nginx/conf.d/site.conf,这样这个配置文件就被添加到docker nginx里了。下面我们执行这个命令生效它:

    1. docker-compose up

    配置PHP

    现在我们已经把nginx配置好了,接下来配置PHP。首先是拉取官方的PHP7-FPM镜像,并链接到Nginx容器,docker-compose.yml如下:

    1. web:
    2.     image: nginx:latest
    3.     ports:
    4.         – "8080:80"
    5.     volumes:
    6.         – ./code:/code
    7.         – ./site.conf:/etc/nginx/conf.d/site.conf
    8.     links:
    9.         – php
    10. php:
    11.     image: php:7-fpm

    下一步是配置nginx使用php-fpm容器来解析php文件。更新后的site.conf如下:

    1. server {
    2.     index index.php index.html;
    3.     server_name php-docker.local;
    4.     error_log  /var/log/nginx/error.log;
    5.     access_log /var/log/nginx/access.log;
    6.     root /code;
    7.  
    8.     location ~ .php$ {
    9.         try_files $uri =404;
    10.         fastcgi_split_path_info ^(.+.php)(/.+)$;
    11.         fastcgi_pass php:9000;
    12.         fastcgi_index index.php;
    13.         include fastcgi_params;
    14.         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    15.         fastcgi_param PATH_INFO $fastcgi_path_info;
    16.     }
    17. }

    为了测试,让我们在./code目录新建一个index.php,内容如下:

    1. <?php
    2. phpinfo();
    3. ?>

    然后通过ip:8080打开,但出现File not found错误。这是因为PHP容器找不到此文件,我们也需要像nginx一样挂载code目录,更新docker-compose.yml文件如下:

    1. web:
    2.     image: nginx:latest
    3.     ports:
    4.         – "8080:80"
    5.     volumes:
    6.         – ./code:/code
    7.         – ./site.conf:/etc/nginx/conf.d/site.conf
    8.     links:
    9.         – php
    10. php:
    11.     image: php:7-fpm
    12.     volumes:
    13.         – ./code:/code

    最后,启动容器:

    1. docker-compose up

    我们将会看到正常的phpinfo页面
    Nginx