侧边栏壁纸
博主头像
seems 博主等级

学习博客

  • 累计撰写 62 篇文章
  • 累计创建 41 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

linux安装Nginx

seems
2023-09-01 / 0 评论 / 0 点赞 / 25 阅读 / 0 字

在线安装1

下载

nginx下载地址:http://nginx.org/download/

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

解压

tar -zxvf nginx.**.tgz

编译ssl模块需要依赖

yum -y install openssl openssl-devel

编译

./configure --prefix=/usr/java/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module 
  • 出现错误 error:the HTTP gzip module requires the zlib library,直接yum安装yum install -y zlib-devel;也可能遇到的是关于pcre的问题,yum install pcre pcre-devel

安装

make&make install

启动

/usr/java/nginx-1.12.2/sbin/nginx -c /usr/java/nginx-1.12.2/conf/nginx.conf

重启

/usr/java/nginx-1.12.2/sbin/nginx -t  ||  /app/nginx/sbin/nginx -t

在线安装2(推荐)

安装

sudo yum install nginx 

启动

sudo systemctl restart nginx.service
  • 配置文件在/etc/下

离线安装 (rpm安装nginx)

1.安装步骤:

1.打开rpm文件下载地址:rpm下载
2.选择 nginx-1.20.1-1.el7.ngx.x86_64.rpm 下载
3.命令安装 rpm -ivh nginx-*.rpm

安装完成之后的说明:

1.安装完成之后需要按照习惯将下方3个配置文件中的内容进行修改

2.默认文件位置及内容:

  • 1.二进制文件(exe):/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /opt/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /opt/nginx/logs/nginx.pid)"

[Install]
WantedBy=multi-user.target
  • 2.服务配置文件: /etc/init.d/nginx
#!/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
# 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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/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
  • 3.应用配置文件:/etc/nginx/

总模块

user  nginx;
worker_processes  auto;

error_log  /opt/nginx/logs/error.log notice;
pid        /opt/nginx/logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /opt/nginx/logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /opt/nginx/conf/*.conf;
}

80端口

server {
    listen       80;
    server_name  localhost;

    rewrite ^/(.*)$ https://domain:443/$1 permanent;
}

443端口配置

    server {
        listen 443 ssl http2;
        # 1.25.1之后写法
        #listen [::]:443 ssl http2;
        
        server_name `your servename`;

        # 开启gz压缩
        gzip on;
        gzip_disable "msie6";
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 8;
        gzip_buffers 16 8k;
        gzip_min_length 100; 
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

        # 开启访问验证密码
        #auth_basic "nginx basic auth";
        #auth_basic_user_file /opt/nginx/nginx/conf/htpasswd;

        ssl_certificate      /opt/nginx/ssl/ssl.pem;  #证书地址
        ssl_certificate_key  /opt/nginx/ssl/ssl.key;  #证书地址

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

        ssl_session_tickets off;

        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
        ssl_prefer_server_ciphers  on;

        ssl_protocols TLSv1.2 TLSv1.3;

        # 访问时不验证证书
        ssl_stapling on;
        ssl_stapling_verify on;
        ssl_trusted_certificate /opt/nginx/ssl/ssl.pem;
        resolver 8.8.8.8 8.8.4.4 valid=300s;
        resolver_timeout 5s;

        ssl_buffer_size 4k;
        
        location / {
            root /opt/nginx/html/web_front/;
            index index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^(.*)$ /index.html?s=$1 last;
                break;
            }
        }

        location /admin/ {
            alias /opt/nginx/html/admin_front/;
            index index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^(.*)$ /index.html?s=$1 last;
                break;
            }
        }
       
        location ^~ /gg/ {
            client_max_body_size 50m;
            proxy_pass http://ip:port/gg/;
            proxy_set_header Host 127.0.0.1:80;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        include   other/*.conf;
    }

微信域名验证

location ~ /WW_verify_(.*)\.txt{
   default_type text/html;
   return 200 $1;
}

权限设置

chmod a+x /etc/init.d/nginx

设置开机自启

chkconfig --add /etc/init.d/nginx
chkconfig nginx on 

nginx服务命令

systemctl daemon-reload
systemctl start nginx
systemctl status nginx

rpm命令

查看安装依赖rpm -qa|grep nginx
卸载依赖rpm -e nginx

0

评论区