CentOS7安装Nginx

2023-12-13 05:51:20

一、安装依赖

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

二、下载Nginx

1.官网直接下载.tar.gz安装包,地址:nginx: download

2.使用wget命令下载

wget -c https://nginx.org/download/nginx-1.22.1.tar.gz

三、解压

tar -zxvf nginx-1.22.1.tar.gz
cd nginx-1.22.1

四、配置

  1. 不需要SSL

    ./configure --prefix=/usr/local/nginx
    
  2. 需要SSL

    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
    

五、编译安装

make
make install

六、启动Nginx

# 启动
cd /usr/local/nginx/sbin/
./nginx 
 
# 重启
./nginx -s reload

七、Nginx配置

  1. nginx.conf配置

    user  root;
    worker_processes  1;
    
    error_log  /usr/local/nginx/logs/error.log warn;
    #pid        /var/run/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       /usr/local/nginx/conf/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  /usr/local/nginx/logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
        gzip  on;
        gzip_min_length 1k;
        gzip_buffers 4 16k;
        gzip_comp_level 3;
        gzip_types text/plain text/css application/xml application/javascript application/x-javascript text/javascript;
        include /usr/local/nginx/conf/conf.d/*.conf;
    }
    
  2. default.http.conf配置

    server {
        listen       80;
        server_name  xxx.cn www.xxx.cn;
        client_max_body_size 150M;
    
        location / {
            root   /home/busapp/ui/;
            #index  index.html;
            # 解决vue打包项目后刷新404的问题
            try_files $uri $uri/ /index.html =404; 
        }
        location /api/ {
            proxy_pass http://127.0.0.1:27071/;
            proxy_redirect              off;
            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 Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_connect_timeout 60s;
            proxy_read_timeout 60s;
            proxy_send_timeout 60s;
        }
        location /ptapi/ {
            proxy_pass http://127.0.0.1:27071/;
            proxy_redirect              off;
            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 Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_connect_timeout 60s;
            proxy_read_timeout 60s;
            proxy_send_timeout 60s;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    
  3. 配置SSL证书

    # 创建证书目录
    cd /usr/local/nginx/
    mkdir cert
    
    server {
        listen 443 ssl;
        server_name  xxx.cn www.xxx.cn;
    
        ssl_certificate      /usr/local/nginx/7369597_xxx.cn.pem;
        ssl_certificate_key  /usr/local/nginx/7369597_xxx.cn.key;
    
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
    
        root   /home/busapp/ui/;
        index  index.html;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        client_max_body_size 50M;
        location /api/ {
            proxy_pass http://127.0.0.1:27071/;
            proxy_redirect              off;
            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 Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_connect_timeout 60s;
            proxy_read_timeout 60s;
            proxy_send_timeout 60s;
        }
        location /ptapi/ {
            proxy_pass http://127.0.0.1:27071/;
            proxy_redirect              off;
            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 Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_connect_timeout 60s;
            proxy_read_timeout 60s;
            proxy_send_timeout 60s;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    
    

八、设置开机自启

  1. rc.local增加启动代码就可以了。

    vim /etc/rc.local
    

    增加一行 /usr/local/nginx/sbin/nginx

    image-20221206103330491

  2. 设置执行权限

    chmod 755 rc.local
    

    到这里,nginx就安装完毕了。

九、卸载Nginx

  1. 卸载Nginx

    yum remove nginx
    
  2. 查看Nginx是否还存在

    which nginx
    

文章来源:https://blog.csdn.net/qq_33399435/article/details/134958032
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。