nignx的部署安装和反向代理

安装Nginx

#yum update

更新一些库和必要的支持,完了之后去下载一个nginx的最新版,如今我责编的版本是1.7.7:

#wget http://nginx.org/download/nginx-1.13.6.tar.gz

解压缩

#tar -zvxf nginx-1.13.6.tar.gz

#cd nginx-1.13.6

nginx有很多很多编译配置项,但由于我这是第一篇笔记,所以我基本上都使用了默认的配置:

#./configure --with-http_ssl_module --with-http_gzip_static_module

我只加了两个选项,--with-http_ssl_module表示使用ssl模块,--with-http_gzip_static_module表示使用gzip模块,其它更详细的配置就要参考nginx的文档了:http://nginx.org/en/docs/configure.html

如果没configure成功(会显示XXX not found),那是因为有些依赖没有被正确安装.那么先安装一下这些依赖条件,通常是pcre,zlib这些,这么一下就基本上可以了:

#yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

#make

#make install

可执行文件就会被安装在: /usr/sbin/nginx (默认配置)

nginx基本使用

程序位置:/usr/local/nginx/sbin/nginx

配置文件位置:/usr/local/nginx/conf/nginx.conf

启动nginx:

#cd /usr/local/nginx/sbin/

#./nginx

如果运行的时候不带-c参数,那就采用默认的配置文件,即/etc/nginx/nginx.conf

查看运行进程状态:

# ps aux | grep nginx

打开浏览器,访问http://localhost/看看nginx的默认页面:

nignx的部署安装和反向代理

停止nginx:

#./nginx -s stop

重启nginx(配置文件变动后需要重启才能生效):

#./nginx -s reload

检查配置文件是否正确:

#./nginx -t

查看nginx的pid:

cat /usr/local/nginx/logs/nginx.pid

查看nginx版本

$ ./nginx -v

回头看编译配置

# ./nginx -V

Nginx配置

#vi /etc/nginx/nginx.conf

user nginx;

worker_processes 1;

error_log /var/log/nginx/error.log warn;

pid /var/run/nginx.pid;

events {

worker_connections 1024;

}

http {

include /etc/nginx/mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://192.168.5.147:8080;

}

}

}

按上面这样配置按理应该可以访问http://192.168.5.147 显示的应该是147:8080的网页内容,但 就是报错了

于是,这才查看日志

#vi /var/log/nginx/error.log

2017/11/03 05:23:53 [crit] 1331#1331: *12 connect() to 192.168.5.147:8080 failed

(13: Permission denied) while connecting to upstream, client: 192.168.5.65, server: localhost,

request: "GET / HTTP/1.1", upstream: "http://192.168.5.147:8080/", host: "192.168.5.147"

园子中搜“(13: Permission denied) while connecting to upstream, client:”就找到原因,

type=AVC msg=audit(1509701033.988:119): avc: denied { name_connect } for pid=1331

comm="nginx" dest=8080 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_cache_port_t:s0 tclass=tcp_socket

处理:

#setsebool -P httpd_can_network_connect 1

到这里应该就正常了,反向代理的后面设置

下面是设置两个服务器

user nginx;

worker_processes 1;

error_log /var/log/nginx/error.log warn;

pid /var/run/nginx.pid;

events {

worker_connections 1024;

}

http {

include /etc/nginx/mime.types;

default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;

#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

upstream test {

server 192.168.5.182:8888;

server 192.168.5.183:7777;

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://test;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Cookie $http_cookie;

}

}

}

相关推荐