nginx安装配置整理

本文介绍了Nginx的安装与配置过程。包括安装gcc环境及第三方库,下载并安装Nginx包,进行脚本文件管理、chkconfig管理,设置开机自启动,还涉及虚拟主机的配置,如重定向配置等内容。

最近整理笔记,把之前笔记上记的一些东西backup一下;

1、安装gcc环境及第三方库
安装环境:centos7.6 
yum install gcc-c++ 
yum install -y pcre pcre-devel 
yum install -y zlib zlib-devel 
yum -y install pcre  pcre-devel zlib  zlib-devel openssl openssl-devel
2、下载并安装ngnix包
wget http://nginx.org/download/nginx-1.18.0.tar.gz   --1.18版本下载较慢,则选择1.14版本 
tar -xvf nginx-1.14.0.tar.gz -C /usr/local    --解压到安装目录下
在解压后的目录下执行.configure 创建makefile文件,并写入安装目录、pid、lock、log等存储文件位置参数
[root@hao-test nginx-1.14.0]# ./configure \ 
--prefix=/usr/local/nginx \ --安装位置 
--pid-path=/var/run/nginx/nginx.pid \    --运行进程位置 
--lock-path=/var/lock/nginx.lock \    
--error-log-path=/var/log/nginx/error.log \ --error日志存储位置 
--http-log-path=/var/log/nginx/access.log \    --访问日志存储位置 
--with-http_gzip_static_module \ 
--http-client-body-temp-path=/var/temp/nginx/client \ 
--http-proxy-temp-path=/var/temp/nginx/proxy \ 
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ 
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ 
--http-scgi-temp-path=/var/temp/nginx/scgi \ 
--with-http_stub_status_module \ 
--with-http_ssl_module \ --with-file-aio \ 
--with-http_realip_module
安装并编译
[root@hao-test nginx-1.14.0]# make &&make install
安装后安装位置(此处为/usr/local/nginx)存在三个文件夹,分别为html:首页html文件;conf:配置文件;sbin:只执行文件;
在./sbin文件下启动nginx
[root@hao-test sbin]# ./nginx
 [root@hao-test sbin]# ps -aux | grep ngnix 
 [root@hao-test sbin]# ./nginx -s reload -c nginx.conf    --刷新配置文件(平滑重启,如果版本小于0.7.53,则需要使用信号量的方式进行重启,使用kill -HUP 信号量的方式平滑重启) 
 [root@hao-test sbin]# ./nginx -s stop/quit  --关闭nginx 
 [root@hao-test sbin]# ./nginx -s stop/quit --关闭nginx
当然也可以将nginx添加到守护进程并使用以下命令
service nginx start 
service nginx stop 
service nginx status
service nginx restart 
nginx -t -c nginx.conf --验证配置文件
3、配置脚本文件管理

创建并编辑文件/etc/init.d/nginx,添加以下内容(nginx官方文件,注意需修改nginx及NGINX_CONF_FILE路径参数):
http://wiki.nginx.org/RedHatNginxInitScript

#!/bin/sh
#
# 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:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
# pidfile:     /usr/local/nginx/logs/nginx.pid              --修改pidfile文件默认存放路径,否则每次重启会丢失路径,修改格式错误会在执行/etc/init.d/nginx start时卡住
# 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"                       --修改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
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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
4、配置chkconfig 管理
chkconfig --add /etc/init.d/nginx 
chkconfig nginx on                                --设置终端模式开机启动
# service nginx start    启动服务 
# service nginx stop     停止服务 
# service nginx restart  重启服务 
# service nginx status   查询服务的状态 
# service nginx relaod   刷新配置文件
5、设置开机自启动

创建并编辑/lib/systemd/system/nginx.service文件,并加入开机自启动,文件内容如下

[Unit]
Description=nginx service
After=network.target 

[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true 

[Install] 
WantedBy=multi-user.target
# systemctl enable nginx   --加入开机自启 
# systemctl disable nginx

启动等命令

# systemctl start nginx.service          启动nginx服务
# systemctl stop nginx.service           停止服务
# systemctl restart nginx.service        重新启动服务
# systemctl list-units --type=service     查看所有已启动的服务
# systemctl status nginx.service          查看服务当前状态
# systemctl enable nginx.service          设置开机自启动
# systemctl disable nginx.service         停止开机自启动
6、配置虚拟主机
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log  /usr/local/nginx/logs/error.log info;      --debug, info, notice, warn, error, crit  默认为crit

#pid        logs/nginx.pid;
pid         /usr/local/nginx/logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        #server_name  www.test1.com;
        server_name   localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
          
           auth_basic           "www.test.com";       --设置nginx登录验证,密码文件需要使用绝对路径
           auth_basic_user_file /usr/local/nginx/nginx_user.passwd;
        }

        #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   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;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    server{
        listen        81;
        #server_name   www.test2.com;
        server_name    localhost;

        location / {
            root html;
            index index-81.html ;
        }
        #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   html;
        }
    }

    upstream loadbanlancer{
        server 127.0.0.1:80;
        #server 127.0.0.1:81 weight=2;
    }
    server{
        listen        8080;
        server_name   localhost;

        location / {
            proxy_pass http://loadbanlancer;
            index index-80.html;
        }
        #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   html;
        }
    }
upstream reverseproxy{
        server 127.0.0.1:81;
    }
    server{
        listen        8080;
        server_name   www.test3.com;

        location / {
            proxy_pass http://reverseproxy;
            index index-80.html;
        }
        #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   html;
        }
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {                                             --https 及开启并配置ssl证书
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

重定向配置

server {         
  listen       80;         
  server_name  localhost;         
  rewrite  ".*" https://www.baidu.com;          
  location / {             
    proxy_set_header Host $host;             
    proxy_set_header X-Real-Ip $remote_addr;             
    proxy_set_header X-Forwarded-For $remote_addr;         
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值