Centos 中安装与配置Nginx
来源:互联网 发布:淘宝没收到货怎么投诉 编辑:程序博客网 时间:2024/04/29 22:35
本文章来自【知识林】
在服务器管理工作中可能会遇到这几种情况:
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.comNo package nginx available.错误:无须任何处理
- 下载rpm文件:[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;
,注意这里的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
- Nginx Centos安装与配置
- CentOS下nginx的安装与配置
- Centos下nginx的安装与配置
- CentOS下NGINX的安装与配置
- Centos下 Nginx安装与配置
- Centos下 Nginx安装与配置
- Centos下 Nginx安装与配置
- Centos下 Nginx安装与配置
- Centos下 Nginx安装与配置
- Centos下 Nginx安装与配置
- nginx安装与配置在CentOS 6下安装nginx
- Linux中Nginx安装与配置(CentOS-6.5:nginx-1.5.0)
- Linux中Nginx安装与配置详解(CentOS-6.5:nginx-1.5.0)
- centos安装配置nginx
- Centos配置安装Nginx
- centos 安装nginx 配置nginx
- Centos中安装nginx
- MySQL C语言API
- 安卓一键换肤的详解
- 在Eclipse下面使用注释模板自动生成说明
- 第七周 项目二 建立链队列算法库
- element filtername is not allowed here
- Centos 中安装与配置Nginx
- 关于float和double丢失精度问题及解决方案
- oracle privs
- Linux Kernel 3.18.41版本发布
- iptables详解
- [Maven]本地仓库位置设置
- VC++ switch语法中的注意事项
- 把书塞进一个App!应用之星,一键制作属于自己的书架App
- STL学习--queue priority_queue