Centos 中安装与配置Nginx

本文章来自【知识林】

在服务器管理工作中可能会遇到这几种情况:

1、 在同一台服务器上安装了Tomcat、IIS都需要用80端口;

2、 在一个局域网内有几台服务器,每台服务器上都有相应的web应用需要以80端口发布。

像以上这两种情况使用Nginx做反向代理就再好不过了。

Nginx可以很方便的将用户的请求代理到相应的服务器和指定的端口上。

·安装Nginx

安装Nginx非常简单,就一个命令就搞定,只用确定系统中有安装yum工具。

yum install nginx

如果运行上面命令后提示 No package nginx available

[root@localhost conf.d]# yum install nginx
已加载插件:fastestmirror, security
设置安装进程
Loading mirror speeds from cached hostfile
 * base: ftp.sjtu.edu.cn
 * elrepo: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
No package nginx available.
错误:无须任何处理
注意:由于我的系统是Centos 6.8(可使用命令`lsb_release -a`查看)的,

所以这里下载的是6.8的rpm,请根据自己的操作系统版本下载对应的rpm文件。

·配置Nginx

主文件配置

Nginx的主配置文件是:/etc/nginx/nginx.conf
做反向代理不需要对主配置文件做过多修改,如果不想对nginx用户做权限调整,则只需要在nginx.conf里面将user nginx修改为user root即可;
可修改worker_processes的值,默认为1,该值一般服务器的cpu数;

Nginx反向代理配置

在主配置文件(nginx.conf)最末可以看到这样的配置:

include /etc/nginx/conf.d/*.conf

这表示Nginx会自动包含/etc/nginx/conf.d目录下的所有以.conf结尾的配置文件。

举个例子:
我需要将www.zslin.com 和 zslin.com以及zslin.com 下的所有二级域名代理到本机的tomcat服务上(tomcat的端口是8080),只需要以下几个简单步骤:

· 创建配置文件

在/etc/nginx/conf.d目录下提供了一个默认的配置文件default.conf

cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/zslin.conf

拷贝一份default.conf并重命名为zslin.conf,名称可以任意,但必须以.conf结尾(因为在主配置文件里面配置了只载入.conf文件)

zslin.conf内容如下:

server {
    #说明监听80端口
    listen       80;

    #访问的域名,多个域名用空格隔开
    server_name  zslin.com www.zslin.com *.zslin.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    charset utf8;

    location / {
        #将请求代理到配置的8080端口上
        proxy_pass  http://localhost:8080;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

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

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

关键代码是proxy_pass http://localhost:8080;如果要代理在局域网内的其他服务器,可配置为:proxy_pass http://IP:PORT;,注意这里的IP是指局域网内的服务器IP地址,PORT是指对应服务器上应用的端口号。

·常见错误

我在配置完Nginx后,启动的时候总是无法正常访问到我的web应用,查看日志文件(/var/log/nginx/error.log)后发现:

connect() to 127.0.0.1:8080 failed (13: Permission denied)

从这个错误信息中可以看出是访问权限的问题,解决办法如下:

vi /etc/nginx/nginx.conf
#user nginx;
user root;

其实就是将nginx的用户修改为root用户即可,当然也可以为nginx用户增加相应的权限。

本文章来自【知识林】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值