• CentOS配置Nginx官方Yum源及安装Ngnix


    一、前置操作

    为了排除其它因素干扰,可以先暂时关闭防火墙和SELinux,待成功后再逐一开启

    # 关闭防火墙
    systemctl stop firewalld.service
    # 关闭SELinux
    setenforce 0
    

    二、Nginx官方yum源配置

    新建repo文件

    vim /etc/yum.repos.d/nginx.repo
    

    根据实际版本、架构配置yum源

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

    三、安装启动Nginx

    运行命令安装Nginx

    yum install -y nginx.x86_64
    

    启动Nginx

    systemctl start  nginx.service
    

    访问Nginx,若返回"Welcome to nginx!"字眼则安装成功

    curl 'http://127.0.0.1'
    

    四、配置Nginx

    1、反向代理

    根据域名转发端口,如访问a.com转发到内网的127.0.0.1:8080

    备份默认配置文件,新建配置文件a.conf

    mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
    touch /etc/nginx/conf.d/a.conf
    

    a.conf文件配置如下

    server {
        # 监听80端口
        listen       80;
        # 监听a.com域名
        server_name  a.com;
    
        # 记录入口日志
        access_log  /var/log/nginx/a.access.log  main;
        # 记录错误日志
        error_log  /var/log/nginx/a.error.log;
    
        location / {
            # 反向代理
            proxy_pass http://127.0.0.1:8080;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
    

    此时访问a.com会转发到内网127.0.0.1:8080上

    五、其它

    其它可能会用到的命令

    # CentOS7 永久开放80端口
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --reload
    
    # SELinux 允许通过网络连接到httpd服务(解决'... connect() to 127.0.0.1:8080 failed (13: Permission denied) while connecting to upstream ...'问题)
    setsebool -P httpd_can_network_connect 1
    

    作者:Emile

    个人主页:http://www.guanjianzhuo.com/

    欢迎访问、评论、留言!

  • 相关阅读:
    3月4号—3月20号的计划
    Codeforces Round #344 (Div. 2) D. Messenger kmp水题
    Codeforces Round #344 (Div. 2) C. Report 水题
    整数三分(模板)
    Codeforces Round #344 (Div. 2) E. Product Sum 三分
    hdu3276 Graph and Queries 离线+treap
    bzoj1588: [HNOI2002]营业额统计 treap
    hdu5002 tree LCT
    bzoj2594 [Wc2006]水管局长数据加强版 离线+LCT维护边权
    bzoj2002 弹飞绵羊 LCT
  • 原文地址:https://www.cnblogs.com/guanjianzhuo/p/10926465.html
Copyright © 2020-2023  润新知