CentOS 7 服务器安装&配置Nginx

2630 3 年前
Nginx (engine x) 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器。。 本例演示 CentOS 7 下安装和配置 Nginx 的基本步骤。

Nginx安装

添加RPM包进行安装

#添加Nginx包
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#安装
sudo yum -y install nginx

启动Nginx并设置为开机启动

//启动服务
sudo systemctl start nginx

//设置nginx开机启动
sudo systemctl enable nginx

//重启服务
$ sudo systemctl restart nginx

//重新加载,因为一般重新配置之后,不希望重启服务,这时可以使用重新加载。
$ sudo systemctl reload nginx

Nginx常用配置

配置文件说明

1、全局配置文件:/etc/nginx/nginx.conf
2、默认配置文件:/etc/nginx/conf.d/default.conf

/etc/nginx/目录下新建文件sh-dsfs.com.8080.conf,内容如下:

server {
    listen 80;
    server_name sh-dsfs.com;
    return 301 http://www.sh-dsfs.com$request_uri;
}
server {
    listen       80;
    server_name  www.sh-dsfs.com;
    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}

重载nginx

sudo systemctl reload nginx
分类栏目
© 2018邮箱:11407215#qq.comGitHub沪ICP备12039518号-6