CentOS 6.5 64位下安装nginx,配置反向代理
2016-06-07 20:05
791 查看
一、环境
操作系统:CentOS 6.5 64位
软件环境:nginx
安装方式:使用yum安装
开放防火墙
SELINUX关闭
二、安装、配置NGINX
1、更新软件
[root@Init ~]# yum update -y
更新完成之后,重启操作系统
[root@Init ~]# init 6
2、安装NGINX
[root@Init ~]# yum install nginx -y
3、配置NGINX(在我的这个环境中,配置文件如下)
#user nobody;
#worker_processes 5;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#error_log /dev/null;
#pid logs/nginx.pid;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 51200;
}
#include tcp.conf;
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$server_addr;" "$upstream_addr "';
#access_log logs/access.log main;
access_log /dev/null;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
#keepalive_timeout 0;
keepalive_timeout 3600s;
gzip on;
upstream api{
#train api
server 10.117.188.44:8001 max_fails=3;
server 10.51.19.21:8001 max_fails=3;
}
server {
listen 80;
server_name api.Init.com;
location / {
proxy_next_upstream error timeout http_500 http_502 http_504;
proxy_redirect off;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_connect_timeout 90s;
proxy_send_timeout 180s;
proxy_read_timeout 180s;
#proxy_buffers 32 4K;
proxy_pass http://api;
}
}
##########################vhost#####################################
#include vhost/*.conf;
}
4、启动NGINX服务
[root@Init nginx]# service nginx status
三、配置防火墙
######################################
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
#####################################
把文本框内容写入到/etc/sysconfig/iptables,覆盖原来的内容(如果有的话)。
[root@Init nginx]# service iptables start #启动防火墙
[root@Init nginx]# iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT #开放nginx的80端口
[root@Init nginx]# service iptables save #保存防火墙的配置
四、设置开机启动
[root@Init nginx]# chkconfig nginx on #配置nginx服务开机自起
[root@Init nginx]# chkconfig iptables on #配置iptables服务开机自起
五、测试反向代理是否生效
在两台后端真实服务器上配置不同的页面,然后打开nginx所在服务器的地址http://IP/
操作系统:CentOS 6.5 64位
软件环境:nginx
安装方式:使用yum安装
开放防火墙
SELINUX关闭
二、安装、配置NGINX
1、更新软件
[root@Init ~]# yum update -y
更新完成之后,重启操作系统
[root@Init ~]# init 6
2、安装NGINX
[root@Init ~]# yum install nginx -y
3、配置NGINX(在我的这个环境中,配置文件如下)
#user nobody;
#worker_processes 5;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#error_log /dev/null;
#pid logs/nginx.pid;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 51200;
}
#include tcp.conf;
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$server_addr;" "$upstream_addr "';
#access_log logs/access.log main;
access_log /dev/null;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
#keepalive_timeout 0;
keepalive_timeout 3600s;
gzip on;
upstream api{
#train api
server 10.117.188.44:8001 max_fails=3;
server 10.51.19.21:8001 max_fails=3;
}
server {
listen 80;
server_name api.Init.com;
location / {
proxy_next_upstream error timeout http_500 http_502 http_504;
proxy_redirect off;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_connect_timeout 90s;
proxy_send_timeout 180s;
proxy_read_timeout 180s;
#proxy_buffers 32 4K;
proxy_pass http://api;
}
}
##########################vhost#####################################
#include vhost/*.conf;
}
4、启动NGINX服务
[root@Init nginx]# service nginx status
三、配置防火墙
######################################
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
#####################################
把文本框内容写入到/etc/sysconfig/iptables,覆盖原来的内容(如果有的话)。
[root@Init nginx]# service iptables start #启动防火墙
[root@Init nginx]# iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT #开放nginx的80端口
[root@Init nginx]# service iptables save #保存防火墙的配置
四、设置开机启动
[root@Init nginx]# chkconfig nginx on #配置nginx服务开机自起
[root@Init nginx]# chkconfig iptables on #配置iptables服务开机自起
五、测试反向代理是否生效
在两台后端真实服务器上配置不同的页面,然后打开nginx所在服务器的地址http://IP/
相关文章推荐
- squid快速编译安装
- Nginx作为反向代理时传递客户端IP的设置方法
- nginx实现反向代理负载均衡
- 反向代理服务器的工作原理
- 正向代理与反向代理
- nginx反向代理安装配置、服务器状态监控(健康检查)
- 代理服务器与反向代理服务器的区别
- 怎么理解反向代理(reverse proxy)的反向?
- squid代理服务器
- Nginx +iis反向代理
- Squid zph-qos 选项
- Nginx安装配置+反向代理(ubuntu下)
- Nginx反向代理websocket配置实例
- 反向代理
- Squid安装和反向代理配置简明指南
- 编译安装nginx并实现反向代理负载均衡和缓存功能
- 正向代理与反向代理的区别【Nginx读书笔记】
- 如何简单理解ngnix的反向代理
- Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
- squid代理