您的位置:首页 > 运维架构 > Nginx

Centos 中安装与配置Nginx

2016-10-21 10:34 357 查看
本文章来自【知识林】

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

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.
错误:无须任何处理


下载rpm文件:[wget (wget]http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm](wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm);

安装:
rpm -ivh epel-release-6-8.noarch.rpm


再次运行:
yum install nginx
即可安装。

注意:由于我的系统是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;[/code],注意这里的
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用户增加相应的权限。

本文章来自【知识林】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  centos nginx