CentOS 安装 Nginx

509人浏览 / 0人评论

参考

https://blog.csdn.net/qq_31348479/article/details/128718566

https://blog.csdn.net/qq_33381971/article/details/123328191

安装

yum安装

# 安装
yum -y install nginx

# 添加到服务
systemctl enable nginx

# 启动
systemctl start nginx

# 停止
systemctl stop nginx

# 查看状态
systemctl status nginx

编译安装

安装

# 安装所需库
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

# 使用wget下载nginx
yum install wget
wget https://nginx.org/download/nginx-1.9.9.tar.gz

# 解压
tar -zxvf nginx-1.9.9.tar.gz

# 配置Nginx编译环境
mkdir /usr/local/nginx
cd nginx-1.9.9
## 不需要ssl
./configure --prefix=/usr/local/nginx
## 需要ssl
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

# 编译&安装
make & make install

# 添加到系统环境
vim  /etc/profile
export PATH=/usr/local/nginx/sbin:$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH

# 设置开机启动
vim /etc/rc.local
## 增加一行 /usr/local/nginx/sbin/nginx
## 设置执行权限
chmod 755 /etc/rc.local

常用命令

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

# 停止
nginx -s stop

# 重新加载
nginx -s reload

# 查看nginx进程
ps -ef|grep nginx

# 卸载
yum remove nginx

# 查看nginx是否还存在
which nginx

配置

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;
}

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;
        }
    }

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;
        }
    }

全部评论