Nginx的安装与配置-使用GeoIP2模块

nginx安装在公网IP为x.x.x.x,私网IP为x1.x1.x1.x1的服务器上。使用GeoIP2模块来进行安装配置nginx是在服务器内网部署的,所以解析不了IP区域信息。

安装libmaxminddb

第一步,下载libmaxminddb安装包

登录官方下载地址:https://github.com/maxmind/libmaxminddb/releases下载libmaxminddb安装包。也可以使用wget命令下载。

命令:cd /usr/local/src/

wget https://github.com/maxmind/libmaxminddb/releases/download/1.7.1/libmaxminddb-1.7.1.tar.gz

第二步,解压libmaxminddb安装包

命令:tar zxvf libmaxminddb-1.7.1.tar.gz

第三步,预编译libmaxminddb

命令:cd /usr/local/src/libmaxminddb-1.7.1

./configure

第四步,编译及安装libmaxminddb

命令:make && make install

第五步,配置动态库

命令:echo "/usr/local/lib" >> /etc/ld.so.conf

第六步,加载动态库

命令:ldconfig

下载GeoIP2模块

第一步,下载GeoIP2模块。

登录官方下载地址:https://github.com/leev/ngx_http_geoip2_module/releases下载GeoIP2模块。也可以使用wget命令下载。

命令:cd /usr/local/src/

wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz

第二步,解压GeoIP2模块。

命令:tar zxvf 3.4.tar.gz

nginx安装

第一步,安装编译工具及库文件。

命令:yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel pcre pcre-devel

第二步,安装Nginx。

# 下载Nginx安装包

命令:cd /usr/local/src/

wget http://nginx.org/download/nginx-1.18.0.tar.gz

# 解压Nginx安装包

命令:tar zxvf nginx-1.18.0.tar.gz

# 编译安装Nginx

命令:cd nginx-1.18.0

./configure \

--prefix=/usr/local/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 \

--pid-path=/var/run/nginx.pid \

--lock-path=/var/run/nginx.lock \

--http-client-body-temp-path=/var/tmp/nginx/client \

--http-proxy-temp-path=/var/tmp/nginx/proxy \

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

--http-scgi-temp-path=/var/tmp/nginx/scgi \

--with-pcre \

--with-http_v2_module \

--with-http_ssl_module \

--with-http_realip_module \

--with-http_addition_module \

--with-http_sub_module \

--with-http_dav_module \

--with-http_flv_module \

--with-http_mp4_module \

--with-http_gunzip_module \

--with-http_gzip_static_module \

--with-http_random_index_module \

--with-http_secure_link_module \

--with-http_stub_status_module \

--with-http_auth_request_module \

--add-module=/usr/local/src/ngx_http_geoip2_module-3.4 \

--with-mail \

--with-mail_ssl_module \

--with-file-aio \

--with-http_v2_module \

--with-threads \

--with-stream \

--with-stream_ssl_module \

--with-ipv6

make && make install

# 查看nginx版本

命令:/usr/sbin/nginx -v

或    /usr/sbin/nginx -V

若结果显示“nginx version: nginx-1.18.0”,则nginx安装完成。

nginx配置

第一步,创建 Nginx 运行使用的用户nginx。

命令:useradd -s /sbin/nologin -M nginx

( Nginx 服务的默认用户是 nobody ,为了安全更改为 nginx,在配置文件中启用user nginx nginx;)

第二步,修改nginx.conf配置文件。

nginx.conf路径为/etc/nginx/nginx.conf。nginx.conf内容如下:

user nginx nginx;  #用户名设置为刚刚创建的用户名
worker_processes  4; #允许生成的进程数,默认为1
worker_cpu_affinity 0001 0010 0100 1000;
error_log  /var/log/nginx/error.log info; #日志位置和级别
pid      /var/run/nginx.pid; #指定nginx进程运行文件存放地址
worker_rlimit_nofile 102400; #最大连接数,默认为512
events {
    use epoll; #事件驱动模型
    worker_connections 102400; #最大连接数,默认为512
    accept_mutex off; #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
}
http
{
    include mime.types;
    default_type application/octet-stream;
    client_max_body_size 20m;
    ...
    server
    { 
       listen 80;  #监听端口
       server_name localhost;  #域名,当前ip地址
       location / {
             root   /usr/local/nginx/html;
             index  index.html index.htm index.php;
       }
       ...
}

第三步,检查配置文件nginx.conf的正确性。

命令:/usr/sbin/nginx -t

若结果显示“nginx: [emerg] mkdir() "/var/tmp/nginx/client" failed (2: No such file or directory)  nginx: configuration file /etc/nginx/nginx.conf test failed”,则说明服务无法启动。可以使用命令“mkdir -p /var/tmp/nginx”创建目录,然后再次运行命令“/usr/sbin/nginx -t”就可以了。

若结果显示“nginx: configuration file /etc/nginx/nginx.conf test is successful”,则说明nginx安装和配置成功。

nginx启动和访问站点

第一步,启动nginx。

命令:/usr/sbin/nginx

第二步,检查是否已经启动。(查看是否有进程)

命令:ps -ef | grep nginx

结果的第一行显示“nginx:master process”,nginx已经启动。

注意:nginx:master process后面有一个路径,这就是nginx的安装路径。

第三步,访问站点。

从浏览器访问已经配置好的站点IP,如果页面显示“Welcome to nginx!”,则说明Nginx已经安装及配置好了。

nginx关闭和重启

第一步,关闭nginx。

命令:/usr/sbin/nginx -s stop

第二步,配置文件修改后,需要指定配置文件进行重启。

如果nginx服务已经停止,那就需要把nginx服务启动。

命令:/usr/sbin/nginx  -c /etc/nginx/nginx.conf

重启nginx服务必须是在nginx服务已经启动的情况下进行,因为这时,/var/run中存在nginx.pid文件。

命令:/usr/sbin/nginx  -s  reload

不进入nginx根目录即可进行相应的操作

第一种方法:

第一步,找到nginx所在的安装目录/usr/local/nginx/sbin,这个目录下有一个名为nginx的文件。

第二步,创建一个软链接放在全局目录中。相当于在全局环境中设置了一个文件指向依赖的环境目录中。

命令:cd /usr/local/bin/

ln -s /usr/sbin/nginx nginx

现在不进入nginx根目录输入命令,不会再提示command not found。

第二种方法:

第一步,新建nginx启动脚本代码。

在文件夹/etc/init.d中新建名为nginx的文件,然后写入下面代码成为脚本文件。代码如下:

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# it is v.0.0.2 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/sbin/nginx

nginx_config=/etc/nginx/nginx.conf

nginx_pid=/var/run/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi

   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

   return $RETVAL

}

# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        killproc $nginxd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

}

# reload nginx service functions.

reload() {

    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo

}

# See how we were called.

case "$1" in

start)

        start

        ;;

stop)

        stop

        ;;

reload)

        reload

        ;;

restart)

        stop

        start

        ;;

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1

esac

exit $RETVAL

第二步,给予/etc/init.d/nginx文件权限。

命令:chmod +x /etc/init.d/nginx

# 设置开机自启

命令:chkconfig --add nginx

chkconfig nginx on

# 检查nginx命令

命令:service nginx

Usage: nginx {start|stop|restart|reload|status|help}

第三步,检查一下脚本是否有用。

命令:/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是否成功自动启动。

与“nginx启动和访问站点”中的第二步和第三步一样操作。

命令:shutdown -r now  #立刻重启

或    reboot           #立刻重启

或    init 6           #立刻重启

或    shutdown -r 10   #过10分钟自动重启

步,nginx启动、关闭以及重启命令。

命令:ps -ef | grep nginx

systemctl start nginx

systemctl stop nginx

systemctl reload nginx

service nginx start

service nginx stop

service nginx reload

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 nginx使用 GeoIP2 模块,需要按照以下步骤进行配置: 1. 下载 GeoIP2 数据文件 首先需要从 MaxMind 网站下载 GeoIP2 数据文件。可以选择免费的 Lite 版本或付费的更高级版本。下载后将文件保存到服务器上。 2. 安装 GeoIP2 模块 要在 nginx使用 GeoIP2 模块,需要先安装该模块。可以通过以下命令安装: ``` sudo apt-get install nginx-module-geoip2 ``` 3. 配置 nginx.conf 文件 在 nginx.conf 文件中添加以下配置: ``` http { geoip2 /path/to/GeoIP2-City.mmdb { auto_reload 60m; $geoip2_data_city_name city names en; $geoip2_data_country_name country names en; } server { listen 80; server_name example.com; set $city "-"; set $country "-"; if ($geoip2_data_city_name != "") { set $city $geoip2_data_city_name; } if ($geoip2_data_country_name != "") { set $country $geoip2_data_country_name; } location / { add_header X-City $city; add_header X-Country $country; } } } ``` 在上述配置中,`/path/to/GeoIP2-City.mmdb` 是 GeoIP2 数据文件的路径。`auto_reload` 表示每隔 60 分钟自动重新加载数据文件。 `$geoip2_data_city_name` 和 `$geoip2_data_country_name` 是 nginx 变量,用于保存从 GeoIP2 数据文件中获取的城市和国家名称。在 server 块中,通过 if 语句将这些变量赋值给 `$city` 和 `$country` 变量。 最后,在 location 块中,可以通过 `add_header` 命令将城市和国家名称添加到 HTTP 响应头中。 4. 重启 nginx 服务 最后,重新启动 nginx 服务以使配置文件生效: ``` sudo service nginx restart ``` 这样就完成了 nginxGeoIP2 模块配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值