centos7 安装配置nginx

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> hot3.png

# nginx 配置
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

tar -zvxf /opt/packages/nginx.tar.gz -C /usr/local

link /usr/lib64/libpcre.so.1        /lib64/libpcre.so.0

#创建日志目录
sudo mkdir /var/log/nginx
chmod -R 777 /var/log/nginx
chmod -R 777 /usr/local/nginx/logs

增加启动脚本
vim /etc/rc.d/init.d/nginxd
```text
#!/bin/bash
#
# chkconfig: 2345 21 79
# description: nginx is a World Wide Web server. It is used to serve
# /etc/init.d/nginxd
# chmod 755 nginxd
# cp nginxd /etc/init.d/
# ln -s /etc/init.d/nginxd /etc/rc.d/init.d/nginxd
# chkconfig --add nginxd
# chkconfig --level 2345 nginxd on
# Nginx is a World Wide Web server.

#source /etc/profile

nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf
case $1 in
start)
    echo -n "Starting Nginx"
    $nginx -c $conf
    echo " done"
;;
stop)
    echo -n "Stopping Nginx"
    killall -9 nginx
    echo " done"
;;
test)
    $nginx -t -c $conf
;;
reload)
    echo -n "Reloading Nginx"
    $nginx -s reload
    echo " done"
;;
restart)
    echo -n "Restart Nginx"
    $0 stop
    sleep 1
    $0 start
    echo " done"
;;
show)
    ps -aux|grep nginx
;;
*)
echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
;;
esac

```
chmod +x /etc/rc.d/init.d/nginxd
chkconfig --add nginxd
chkconfig nginxd on

#修改配置nginx配置文件
vim /usr/local/nginx/conf/conf.d/mohurd_ow.conf

```text
upstream mohurd_ow_foreground {
     server 172.16.0.142:3801 weight=1;
     server 172.16.0.140:3801 weight=1;
}


server {
    listen       80;
    server_name  _;

    location / {
        proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
        proxy_pass http://mohurd_search;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto  $scheme;
        proxy_set_header   X-Forwarded-Port  $server_port;
    }

      location ~* .*\.(txt|wps|doc|docx|xls|xlsx|pdf|rar|gz|zip|exe|ppt|pptx)(\?.*)?$ {
        root /data/mohurd/static/;
        if ($request_uri ~* ^.*\/(.*)\.(txt|wps|doc|docx|xls|xlsx|pdf|rar|gz|zip|exe|ppt|pptx)(\?n=([^&]+))$) {
          add_header Content-Disposition "attachment;filename=$arg_n.$2";
        }
        access_log off;
         if ($request_uri ~* ".(css|js|png|jpg|jpeg|gif|bmp|ico|woff|woff2|mp3|ttf|svg|swf)\?v=.+$") {
                         expires 30d;
                         add_header Cache-Control "public";
         }
        
        if ($request_uri ~* "(\.|-)(\w{7}|\w{20}|\w{32})\.(css|js|png|jpg|jpeg|gif|bmp|ico|woff|woff2|mp3|ttf|svg|swf)$") {
             expires 30d;
             add_header Cache-Control "public";
        }
      }
 
}

检测
如果有异常:找不到libpcre.so.0
  find / -type f  -name '*libpcre.so*'
  cd /lib64  如果存在libpcre.so.1
优先用 link /usr/lib64/libpcre.so.1        /lib64/libpcre.so.0
备选ln -s  /usr/lib64/libpcre.so.1.2.0  /lib64/libpcre.so.0


```

## 操作nginx命令
```text
service nginxd start
service nginxd reload

```

你可能感兴趣的:(centos7 安装配置nginx)