Centos 中安装与配置Nginx
2016-10-21 10:34
357 查看
本文章来自【知识林】
在服务器管理工作中可能会遇到这几种情况:
1、 在同一台服务器上安装了Tomcat、IIS都需要用80端口;
2、 在一个局域网内有几台服务器,每台服务器上都有相应的web应用需要以80端口发布。
像以上这两种情况使用Nginx做反向代理就再好不过了。
Nginx可以很方便的将用户的请求代理到相应的服务器和指定的端口上。
如果运行上面命令后提示
下载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);
安装:
再次运行:
做反向代理不需要对主配置文件做过多修改,如果不想对nginx用户做权限调整,则只需要在
可修改
这表示Nginx会自动包含
举个例子:
我需要将www.zslin.com 和 zslin.com以及zslin.com 下的所有二级域名代理到本机的tomcat服务上(tomcat的端口是8080),只需要以下几个简单步骤:
拷贝一份default.conf并重命名为zslin.conf,名称可以任意,但必须以
zslin.conf内容如下:
关键代码是
在服务器管理工作中可能会遇到这几种情况:
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用户增加相应的权限。
本文章来自【知识林】
相关文章推荐
- centOS5.6 下的nginx+mysql+php+php-fpm安装与配置
- centos 安装nginx 配置nginx
- nginx在centos系统下安装以及nginx的伪静态安装配置
- CentOS-6.3安装配置Nginx
- CentOS6.4安装配置nginx+pcre+php/fpm
- LNMP= CentOS+Nginx+MySQL+PHP 安装配置及简易建站笔记(持续更新中)
- Centos下nginx的安装与配置
- nginx安装与配置在CentOS 6下安装nginx
- centos nginx基本安装配置
- centos 6.3安装nginx 配置文件服务器
- 详解 CentOS 安装配置 Nginx 0.8.33+PHP 5.3.1+php-fpm+MySQL 全过程【转】
- Centos 6.0/ Nginx 安装与配置
- Centos 下 Tomcat+Nginx 安装与配置
- CentOS 6.3 编译安装 配置 nginx server php mysql c gcc g++ c++等
- Centos下Nginx+PHP+MySql的安装和配置(综合整理)
- centos下nginx+tomcat安装配置 nginx结合tomcat
- CentoS下Django1.3 & Nginx(FastCGI & flup) & MySQL安装配置详解
- Centos 下源码安装配置Nginx +PHP + fastcgi+mysql+MemCached
- centos 安装php-fpm , nginx二级域名配置 ,但为什么必须要 域名提供商 哪里解析新的二级域名一下 才能用呢?