美文网首页
Nginx安装与配置

Nginx安装与配置

作者: 今天_吃_什么 | 来源:发表于2019-01-30 11:34 被阅读0次

    操作环境

    • 云机平台:搬瓦工
    • 操作系统:CentOS 7.6
    • 安装时间:20190129

    配置镜像源

    增加一个nginx的源nginx.repo(官网最新版本相对更新一点,此步骤可跳过)

    # vi /etc/yum.repos.d/nginx.repo  //编辑源文件
    

    源文件的内容

    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=0
    enabled=1
    

    安装nginx

    查找nginx源

    # yum list nginx  
    

    全自动安装nginx

    # yum -y install nginx
    

    开机启动设置

    # systemctl enable nginx
    # systemctl daemon-reload
    

    重启服务器之后,查看是否安装成功并且开机已启动

    # systemctl status nginx
    

    配置nginx

    已经安装nginx.x86_64 1:1.14.2-1.el7_4.ngx
    默认nginx配置文件在 /etc/nginx

    # cd  /etc/nginx
    # ls -l
    total 40
    drwxr-xr-x 2 root root 4096 Jan 29 22:10 conf.d
    -rw-r--r-- 1 root root 1007 Dec  4 10:03 fastcgi_params
    -rw-r--r-- 1 root root 2837 Dec  4 10:03 koi-utf
    -rw-r--r-- 1 root root 2223 Dec  4 10:03 koi-win
    -rw-r--r-- 1 root root 5170 Dec  4 10:03 mime.types
    lrwxrwxrwx 1 root root   29 Jan 28 21:19 modules -> ../../usr/lib64/nginx/modules
    -rw-r--r-- 1 root root  643 Dec  4 10:01 nginx.conf
    -rw-r--r-- 1 root root  636 Dec  4 10:03 scgi_params
    -rw-r--r-- 1 root root  664 Dec  4 10:03 uwsgi_params
    -rw-r--r-- 1 root root 3610 Dec  4 10:03 win-utf
    

    实际起效的配置是 /etc/nginx 目录下的 nginx.conf 文件,但是被代理服务器的配置放在 /etc/nginx/conf.d 的default.conf 文件
    本人配置nginx是为了给koa2后台做端口转发,3000转80 ,所以修改 /etc/nginx/conf.d 目录下的 default.conf 文件
    先备份,然后编辑

    # cp default.conf default.conf.bak
    # vi default.conf
    

    修改文件为

    server {
        listen       80;
        listen       [::]:80;
        server_name  localhost;
        location / {
            proxy_pass http://127.0.0.1:3000;
            proxy_set_header Host $host;
            proxy_set_header Cookie $http_cookie;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    

    保存后重启nginx

    nginx -s reload
    

    相关文章

      网友评论

          本文标题:Nginx安装与配置

          本文链接:https://www.haomeiwen.com/subject/nnuqsqtx.html