如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理服务器配置

& nginx反向代理配置
nginx反向代理配置
nginx作为web服务器一个重要的功能就是反向代理。
当然你也可以使用,本是介绍如何配置nginx的反向代理。
nginx反向代理的指令不需要新增额外的模块,默认自带proxy_pass指令,只需要修改配置文件就可以实现反向代理。
配置前的准备工作,后端跑apache服务的ip和端口,也就是说可以通过http://ip:port能访问到你的网站。
然后就可以新建一个vhost.conf,加入如下内容,记得修改ip和域名为你的ip和域名。
修改nginx.conf,添加 include quancha.conf 到http{}段, reload nginx就可以了。
quancha.conf文件如下:
## Basic reverse proxy server ##
## Apache backend for
upstream apachephp
server ip:8080; #Apache
listen 80;
access_log
logs/quancha.access.
logs/quancha.error.
index.html index.htm index.
## send request back to apache ##
location / {
proxy_pass
#Proxy Settings
proxy_set_header
proxy_set_header
proxy_set_header
X-Forwarded-For
$proxy_add_x_forwarded_
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout
proxy_send_timeout
proxy_read_timeout
proxy_buffer_size
proxy_buffers
proxy_busy_buffers_size
proxy_temp_file_write_size 64k;
12345678910111213141516171819202122232425262728293031323334353637
## Basic reverse proxy server #### Apache backend for
##upstream apachephp&&{&&&&server ip:8080; #Apache}&## Start
##server {&&&&listen 80;&&&&server_name&&www.quancha.cn;&&&&&access_log&&logs/quancha.access.log&&main;&&&&error_log&&logs/quancha.error.log;&&&&root&& html;&&&&index&&index.html index.htm index.php;&&&&&## send request back to apache ##&&&&location / {&&&&&&&&proxy_pass&&http://&&&&&&&&&#Proxy Settings&&&&&&&&proxy_redirect&&&& off;&&&&&&&&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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;&&&&&&&&proxy_max_temp_file_size 0;&&&&&&&&proxy_connect_timeout&&&&&&90;&&&&&&&&proxy_send_timeout&&&&&&&& 90;&&&&&&&&proxy_read_timeout&&&&&&&& 90;&&&&&&&&proxy_buffer_size&&&&&&&&&&4k;&&&&&&&&proxy_buffers&&&&&&&&&&&&&&4 32k;&&&&&&&&proxy_busy_buffers_size&&&&64k;&&&&&&&&proxy_temp_file_write_size 64k;&& }}## End
除非注明,本站文章均为: 原创,转载请注明本文地址:
- 491,833 views - 332,163 views - 306,634 views - 194,605 views - 194,348 views - 183,525 views - 129,720 views - 114,317 views - 112,294 views - 104,108 viewsCentos 7.3 x64安装nginx-1.13.1nginx-1.13.1 发布日期测试完成日期: by evan.li一、安装必要所需环境:# yum install autoconf automake gcc gcc-c++ libtool make pkgconfig zlib-devel&# yum install pcre pcre-devel php-devel httpd-devel# yum install zlib zlib-devel &# yum install openssl openssl-devel &# &groupadd nginx# &useradd -g nginx nginx# &mkdir /software# &cd /software# wget http://nginx.org/download/nginx-1.13.1.tar.gz# tar xzf nginx-1.13.1.tar.gz &&# cd nginx-1.13.1 &安装相关依赖包[root@ns1 nginx-1.13.1]# &yum install libxml2-devel libxslt-devel gd-devel[root@ns1 nginx-1.13.1]# &yum install perl perl-devel perl-ExtUtils-Embed &libatomic_ops-devel[root@ns1 nginx-1.13.1]# ./configure --user=nginx --group=nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --with-select_module --with-poll_module --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-cpp_test_module &--with-cpu-opt=CPU --with-pcre &--with-pcre-jit &--with-zlib-asm=CPU --with-libatomic --with-debug --with-ld-opt="-Wl,-E" --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi[root@ns1 nginx-1.13.1]#&....成功结果如下checking for atomic_ops library ... foundcreating objs/Makefile&Configuration summary& + using system PCRE library& + using system OpenSSL library& + using system zlib library& + using system libatomic_ops library&& nginx path prefix: "/usr/local/nginx"& nginx binary file: "/usr/sbin/nginx"& nginx modules path: "/usr/local/nginx/modules"& nginx configuration prefix: "/etc/nginx"& nginx configuration file: "/etc/nginx/nginx.conf"& nginx pid file: "/var/run/nginx.pid"& nginx error log file: "/var/log/nginx/error.log"& nginx http access log file: "/var/log/nginx/access.log"& nginx http client request body temporary files: "/var/tmp/nginx/client/"& nginx http proxy temporary files: "/var/tmp/nginx/proxy/"& nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"& nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi"& nginx http scgi temporary files: "/var/tmp/nginx/scgi"&./configure: warning: the "--with-ipv6" option is deprecated[root@ns1 nginx-1.13.1]# make
上一篇: 下一篇:用户名:守住每一天
文章数:192
评论数:885
访问量:2021467
注册日期:
阅读量:1297
阅读量:3317
阅读量:441032
阅读量:1127376
51CTO推荐博文
nginx反向代理配置及优化作者:守住每一天blog:liuyu.bbs:bbs.linuxtone.orgmsn:email:前言:&&&& 由于服务器apache抗不住目前的并发.加上前端squid配置后,问题依然无法解决.而页面程序大部分是动态.无法使用fastcgi来处理.因此想使用nginx做为反向代理apache.整个配置安装过程很简单.在考虑高并发的情况下,在安装前就做了些优化.目前配置能抗住3000以上并发.好像不是特别大哦?呵~~ 但足以~~ 只是还有少量499问题..期待有人跟我讨论解决& 第1部分:安装1 建立用户及组/usr/sbin/groupadd www/usr/sbin/useradd -g www www2 安装pcre 让nginx支持rewrite 方便以后所需wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gztar zxvf pcre-7.8.tar.gzcd pcre-7.8/./configuremake && make install3 安装nginxwget http://sysoev.ru/nginx/nginx-0.7.58.tar.gztar zxvf nginx-0.7.58.tar.gzcd nginx-0.7.58/./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-cc-opt='-O2' --with-cpu-opt=opteronmake && make install#注意上文中的--with-cc-opt='-O2' --with-cpu-opt=opteron 这是编译器优化,目前最常用的是-02 而不是3.后面对应CPU的型号,可参照:http://wiki.gentoo.tw/index.php/HOWTO_CFLAG第2部分:配置及优化配置文件1 nginx.conf 配置文件:user&&&&worker_processes 4;# [ debug | info | notice | warn | error | crit ]error_log&&&&/usr/local/webserver/nginx/logs/nginx_error.log&&&&pid&&&&&&&&&&&&&&&&/usr/local/webserver/nginx/nginx.#Specifies the value for maximum file descriptors that can be opened by this process.worker_rlimit_nofile 51200;events{&&&&&&&&&&&&&&&& worker_connections 51200;}http{&&&&&&&& include&&&&&&&&&&&& mime.&&&&&&&& default_type&&&&application/octet-&&&&&&&& source_charset GB2312;&&&&&&&& server_names_hash_bucket_size 256;&&&&&&&& client_header_buffer_size 256k;&&&&&&&& large_client_header_buffers 4 256k;&&&&&&&& #size limits&&&&&&&& client_max_body_size&&&&&&&&&&&& 50m;&&&&&&&& client_body_buffer_size&&&&&&&&256k;&&&&&&&& client_header_timeout&&&& 3m;&&&&&&&& client_body_timeout 3m;&&&&&&&& send_timeout&&&&&&&&&&&& 3m; #参数都有所调整.目的是解决代理过程中出现的一些502 499错误&&&& &&&&&&&&&&&&&&&& tcp_nopush&&&&&&&&&&&&&&&& keepalive_timeout 120; #参数加大,以解决做代理时502错误&&&&&&&& tcp_&&&&&&&&&&&&&&&& include&&&&&&&&&&&&&&&&&&&&vhosts/upstream.&&&&&&&& include&&&&&&&&&&&&&&&&&&&&vhosts/bbs.linuxtone.
}2 upstream.conf 配置文件(这也是做负载的配置方法)upstream.conf&&&&&&&&&&&&upstream
{&&&&&&&&&&&&&&&& server 192.168.1.4:8099;&&&&&&&&&&&& }3 站点配置文件bbs.linuxtone.confserver&&&& {&&&&&&&&&&&&listen&&&&&&&&&&&& 80;&&&&&&&&&&&&server_name&&&&bbs.linuxtone.&&&&&&&&&&&&charset GB2312;&&&&&&&&&&&&index index.html index.&&&&&&&&&&&&root&&&&/date/wwwroot/linuxtone/;&&&&&&&&&&&&&&&&location ~ ^/NginxStatus/ {&&&&&&&&&&&&&&&&&&&&&&&&stub_&&&&&&&&&&&&&&&&&&&&&&&&access_&&&&&&&&&&&&&&&& }&&&&&&&& location / {&&&&&&&&&&&& root&&&&/date/wwwroot/linuxtone/;&&&&&&&&&&&& proxy_&&&&&&&&&&&& proxy_set_header Host $&&&&&&&&&&&& proxy_set_header X-Real-IP $remote_&&&&&&&&&&&& proxy_set_header REMOTE-HOST $remote_&&&&&&&&&&&& proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_&&&&&&&&&&&& client_max_body_size 50m;&&&&&&&&&&&& client_body_buffer_size 256k;&&&&&&&&&&&& proxy_connect_timeout 30;&&&&&&&&&&&& proxy_send_timeout 30;&&&&&&&&&&&& proxy_read_timeout 60;&&&&&&&&&&&& proxy_buffer_size 256k;&&&&&&&&&&&& proxy_buffers 4 256k;&&&&&&&&&&&& proxy_busy_buffers_size 256k;&&&&&&&&&&&& proxy_temp_file_write_size 256k;&&&&&&&&&&&& proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;&&&&&&&&&&&& proxy_max_temp_file_size 128m;&&&&&&&&&&&& proxy_pass&&&&;&&&&&&&&&&&&}#参数都有所调整.目的是解决代理过程中出现的一些502 499错误&&& #Add expires header for static content&&&& location ~* \.(jpg|jpeg|gif|png|swf)$ {&&&&&&&& if (-f $request_filename) {&&&&&&&&&&&& root /date/wwwroot/linuxtone/;&&&&&&&&&&&& expires&&&&&&&&&&&&1d;&&&&&&&&&&&&&&&&&&&&&&&&}&&&& }&&&&&&&& log_format&&&&access&&&&'$remote_addr - $remote_user [$time_local] "$request" '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& '$status $body_bytes_sent "$http_referer" '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& '"$http_user_agent" $http_x_forwarded_for';&&&&&&&&access_log&&&&/exp/nginxlogs/bbs.linuxtone_access.log&&&&&&&&}注:第二种代理方式 nginx 处理下图片,html等静态的东西.其它动态由apache处理.因此apache也需要做一些参数调整.设置图片等过期时间.缓解请求.如果源与nginx在同一台机器建议使用如下方法:&&&&&&&&&&&&&&&& location / {&&&&&&&&&&&&&&&&&&&&&&&&&&&&proxy_pass&&&&http://192.168.1.4:8099/;&&&&&&&&&&&&&&&&&&&&&&&&&&&&proxy_&&&&&&&&&&&&&&&& }针对不同的目录进行代理把下面的配置放到根目录代理的上面&&&&&&&&&&&&&&&&location /linuxtone/ {&&&&&&&&&&&&&&&&&&&&&&&&&&&&proxy_pass&&&&http://192.168.1.4:8099/linuxtone/;&&&&&&&&&&&&&&&&&&&&&&&&&&&&proxy_&&&&&&&&&&&&&&&& }4 源配置&VirtualHost 192.168.1.4:8099&&&&&&&&&ServerAdmin &&&&&&&&DocumentRoot /date/wwwroot/linuxtone&&&&&&&&ServerName &&&&&&&&ErrorLog logs/linuxtone_error_log&&&& CustomLog "|/usr/local/sbin/cronolog logs/linuxtone_access_log.%Y%m%d" combined&/VirtualHost&&第3部分:源的优化1 apache-mpm.conf&IfModule mpm_prefork_module&&&&&&&&&StartServers&&&&&&&&&&&&&&&&&&&&15&&&&&&&&MinSpareServers&&&&&&&&&&&& 15&&&&&&&&MaxSpareServers&&&&&&&&&&&&30&&&&&&&&ServerLimit&&&&&&&&&&&&&&&& 2536&&&&&&&&MaxClients&&&&&&&&&&&&&&&&&&&&2048&&&&&&&&MaxRequestsPerChild&&&& 1500&/IfModule&2 apache-keepaliveTimeout 120&& #与nginx的保持一至KeepAlive OnMaxKeepAliveRequests 400KeepAliveTimeout 7第4部分:PHP的优化优化一:将PHP由之前的xcache换成eaccelerator1 安装wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2tar jxvf eaccelerator-0.9.5.3.tar.bz2cd eaccelerator-0.9.5.3//usr/local/webserver/php/bin/phpize./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php5/bin/php-configmakemake install注:PHP路径以安装为准!2 配置sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-/"\nextension = "memcache.so"\n#' /etc/php.inised -i 's#output_buffering = Off#output_buffering = On#' /etc/php.inised -i "s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g" /etc/php.ini配置eAccelerator加速PHP:mkdir -p /usr/local/webserver/eaccelerator_cachevi /etc/php.ini按shift+g键跳到配置文件的最末尾,加上以下配置信息:[eaccelerator]zend_extension="/usr/local/php5/lib/php/extensions/no-debug-non-zts-/eaccelerator.so"eaccelerator.shm_size="128"eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"eaccelerator.enable="1"eaccelerator.optimizer="1"eaccelerator.check_mtime="1"eaccelerator.debug="0"eaccelerator.filter=""eaccelerator.shm_max="0"eaccelerator.shm_ttl="300"eaccelerator.shm_prune_period="120"eaccelerator.shm_only="0"press="1"press_level="9"优化二:联系开发重新编译php减少php的模块.以减少php进程所占用内存数.这块尽管影响不大,但也有一定的作用.编译前也可以参照nginx的编译器优化方式安装.第5部分:测试并启动nginxulimit -SHn 51200/usr/local/webserver/nginx/sbin/nginx -t /usr/local/webserver/nginx/sbin/nginx第6部分:nginx日志切割脚本#!/bin/bash# This script run at 00:00# The Nginx logs pathlogs_path="/exp/nginxlogs/"mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/mv ${logs_path}bbs.linuxtone_access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/bbs.linuxtone_access_$(date -d "yesterday" +"%Y%m%d").logkill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`crontab -e 00 00 * * * /bin/bash&&&&/usr/local/webserver/nginx/sbin/cut_nginx_log.sh 本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:未分类┆阅读(0)┆评论(0)
本文收录至博客专题:《》
18:26:55 06:04:27 17:00:31 14:58:51 11:53:19 12:01:28 12:03:00 12:04:20 16:11:57 08:47:00 &&1&
&&页数 ( 1/4 ) &Centos7.2安装Nginx实现负载平衡的方法
作者:淅淅沥沥
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Centos7.2安装Nginx实现负载平衡的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
到官网下载源码文件,地址:,选择最新版本。本人下载的地址为:,可用wget命令下载,也可以在windows系统上下载好再传到linux上。
如果系统默认安装了httpd服务,卸载之。不卸载也没关系,这里只是方便默认80端口的处理。
yum -y remove httpd
tar -xzvf nginx-xxxxxx.tar.gz
安装编译器和依赖库
yum install gcc gcc-c++ zlib-devel pcre-devel openssl-devel openssl-libs openssl -y
如果已经安装,就不必了
安装前配置
cd命令转到解压后的目录下。
./configure --prefix=/usr/local/nginx
这样安装时系统就会把Nginx安装到/usr/local/nginx目录下。
make install
安装完成,接下来配置环境变量以后就不用使用绝对路径来操作Nginx了:
vim /etc/profile.d/http.sh
加入以下内容:
export PATH=/usr/local/nginx/sbin:$PATH
生效配置:
nginx -s 后跟stop、reload来关闭和重载nginx,直接运行nginx则启动服务。& 如果启动时提示端口被占用,则需要找出被占用的进程,或者更改/usr/local/nginx/conf/nginx.conf文件里的侦听端口。
在浏览器上输入
如果出现“Welcome to nginx!”字样,则证明安装成功。如果访问不了,先确认防火墙是否禁止相应端口了。
负载平衡配置示例
worker_processes 2;
#error_log logs/error.
#error_log logs/error.
#error_log logs/error.
logs/nginx.
accept_ #设置网路连接序列化,防止惊群现象发生,默认为on
multi_ #设置一个进程是否同时接受多个网络连接,默认为off
worker_connections 1024;#最大连接数
mime.#文件扩展名与文件类型映射表,此映射表主要用于部署在本nginx上的静态资源
default_type application/octet-
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 logs/access.
#keepalive_timeout 0;
keepalive_timeout 65;#连接超时时间
#【配置1】此配置是[配置4]和[配置5]的结合
#此配置将请求转发到两个WEB服务器,根据客户端IP分配目标主机,同时按权重分配流量
upstream app1 {
server 192.168.14.132:8080 weight=5;
server 192.168.14.133:80 weight=3;
#【配置2】
#默认负载平衡配置,nginx应用HTTP负载平衡来分发请求。
#upstream app1 {
server 192.168.14.132:8080;
server 192.168.14.133:80;
#【配置3】
#最小连接负载平衡配置,nginx将尽量不使用繁忙的服务器,而是将新请求分发给不太忙的服务器。
#upstream app1 {
server 192.168.14.132:8080;
server 192.168.14.133:80;
#【配置4】
#会话持久性配置,使用ip-hash,客户端的IP地址用作散列密钥,
#以确定应为客户端请求选择服务器组中的哪个服务器。
#此方法确保来自同一客户端的请求将始终定向到同一服务器,除非此服务器不可用。
#upstream app1 {
server 192.168.14.132:8080;
server 192.168.14.133:80;
#【配置5】
#加权负载平衡配置,通过使用服务器权重进一步影响nginx负载平衡算法。
#未配置权重的服务器,意味着所有指定的服务器被视为对特定负载平衡方法同等资格。
#upstream app1 {
server 192.168.14.132:8080 weight=3;
server 192.168.14.133:80 weight=2;
server 192.168.14.134:80;
server 192.168.14.135:80;
server {#可配置多个server以监听不同IP和不同端口
80;#监听的端口
server_#监听的服务器
#charset koi8-r;
#access_log logs/host.access.
#反斜杆代表所有连接,此配置目的是将所有连接交给名为app1的upstream代理,实现负载平衡
location / {
proxy_pass http://app1;
#图片文件路径,一般来说,静态文件会部署在本机以加快响应速度
#可配置多个这样的location,满足各种需求
location ~\.(gif|jpg|png)$ {
root /home/root/
location ~\.(iso|zip|txt|doc|docx)$ {
root /home/root/
#error_page 404
# redirect server error pages to the static page /50x.html
error_page
500 502 503 504 /50x.
location = /50x.html {
# FastCGI是CGI全称是“公共网关接口”(Common Gateway Interface)
#对于我来说,使用Tomcat代替即可,请忽略此配置。
#location ~ \.php$ {
fastcgi_pass
127.0.0.1:9000;
fastcgi_index index.
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_
# 添加黑名单,禁止某某访问特定文件
# concurs with nginx's one
#location ~ /\.ht {
# another virtual host using mix of IP-, name-, and port-based configuration
somename:8080;
server_name somename alias another.
location / {
index index.html index.
# HTTPS server
ssl_certificate
ssl_certificate_key cert.
ssl_session_cache
shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_
location / {
index index.html index.
配置完后,记得执行以下命令生效配置
nginx -s reload
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 nginx配置url反向代理 的文章

 

随机推荐