CentOS安装Nginx

一:安装依赖

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.18.0.tar.gz

3.网盘下载

链接:https://pan.baidu.com/s/1C3wfnqAgE2QZgyRPd3zU-w
提取码:6x06

三:解压

tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

四:配置

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

# 启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

# 重启
/usr/local/nginx/sbin/nginx -s reload

七:代理配置

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.default.https.conf配置

# 创建证书目录
cd /usr/local/nginx/conf/
mkdir cert

server {
        listen 443 ssl;
        server_name  xxx.cn www.xxx.cn;

        ssl_certificate      cert/7369597_xxx.cn.pem;
        ssl_certificate_key  cert/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

CentOS安装Nginx_第1张图片

2.设置执行权限

chmod 755 rc.local

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

九:卸载Nginx

1.卸载Nginx

yum remove nginx

2.查看Nginx是否还存在

which nginx

你可能感兴趣的:(centos,nginx,linux)