nginx与apache反向代理

摘要:nginx与apache一起做反向代理,各自的优点就不细说了,反正一个处理并发及静态的牛差差,一个处理动态的牛差差。 想看apache如何运行安装,可以查看我的另一篇centOS7 LAMP安装及注意要点。 1、apache相关配置文件更改端口号 主配置文件: vi /etc/httpd/conf/httpd.conf Listen 81 虚拟主机配置文件: vi /etc/htt

nginx与apache一起做反向代理,各自的优点就不细说了,反正一个处理并发及静态的牛差差,一个处理动态的牛差差。

想看apache如何运行安装,可以查看我的另一篇centOS7 LAMP安装及注意要点。

002UASMrzy7605pjKJv15&690.jpg

1、apache相关配置文件更改端口号

主配置文件:

vi/etc/httpd/conf/httpd.conf Listen81

虚拟主机配置文件:

vi/etc/httpd/conf.d/lock.com.conf <VirtualHost192.168.136.128:81> DirectoryIndexindex.php ServerAdmin2871903572@qq.com DocumentRoot/www/lockcom ServerNamelock.com ServerAliaslock.com <Directory/www/lockcom> OptionsIndexesFollowSymLinks AllowOverrideNone Requireallgranted </Directory> </VirtualHost>

2、nginx安装

用yum来安装吧

yumnginxinstall /bin/systemctlstartnginx.service systemctlstatusnginx.service

应该是nginx欢迎页面

 

3、更改nginx相关配置文件

nginx.conf主配置文件:

主要是proxy_pass做反向代理

如果你在apache虚拟主机用了固定IP地址话,proxy_pass应该是一样的IP地址,如果没有设置,正常用127.0.0.1就可以。

同时建立nginx的虚拟主机配置目录,并在主配置文件下包含:include /etc/nginx/vhost/*.conf;

mkdir/etc/nginx/vhost touch/etc/nginx/vhost/lock.com.conf

下面nginx主配置文件:

nginx.conf

#Formoreinformationonconfiguration,see: #*OfficialEnglishDocumentation:http://nginx.org/en/docs/ #*OfficialRussianDocumentation:http://nginx.org/ru/docs/ usernginx; worker_processesauto; error_log/var/log/nginx/error.log; pid/run/nginx.pid; events{ worker_connections1024; } http{ log_formatmain\'$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.logmain; sendfileon; tcp_nopushon; tcp_nodelayon; keepalive_timeout65; types_hash_max_size2048; include/etc/nginx/mime.types; default_typeapplication/octet-stream; #Loadmodularconfigurationfilesfromthe/etc/nginx/conf.ddirectory. #Seehttp://nginx.org/en/docs/ngx_core_module.html#include #formoreinformation. include/etc/nginx/conf.d/*.conf; server{ #listen80default_server; #listen[::]:80default_server; listen80; server_name_; #root/usr/share/nginx/html; #Loadconfigurationfilesforthedefaultserverblock. #include/etc/nginx/default.d/*.conf; location/{ try_files$uri@apache; } location@apache{ internal; proxy_passhttp://192.168.136.128:81; } location~.*.(php|php5)?${ proxy_passhttp://192.168.136.128:81; } location~.*.(gif|jpg|jpeg|png|bmp|swf|flv|ico)${ expires30d; } location~.*.(js|css)?${ expires7d; } error_page404/404.html; location=/40x.html{ } error_page500502503504/50x.html; location=/50x.html{ } } include/etc/nginx/vhost/*.conf; }

虚拟主机下的配置vhost/lock.com.conf

vi/etc/nginx/vhost/lock.com.conf server{ listen80; server_namelock.com; access_log/www/wwwlogs/lock.com_nginx.logcombined; indexindex.htmlindex.htmindex.jspindex.php; root/www/lockcom; #error_page404/404.html; if($query_string~*".*[;\'<>].*"){ return404; } if($http_user_agent~ApacheBench|WebBench|Jmeter|must-revalidate|Havij){ return503; } location~.*.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)${ valid_referersnoneblockednahehuo.com*.nahehuo.com; if($invalid_referer){ #rewrite^/http://www.lock.com/403.html; return403; } } limit_rate500k; location/{ try_files$uri@apache; } location@apache{ internal; proxy_passhttp://192.168.136.128:81; } location~.*.(php|php5)?${ proxy_passhttp://192.168.136.128:81; } location~.*.(gif|jpg|jpeg|png|bmp|swf|flv|ico)${ expires30d; } location~.*.(js|css)?${ expires7d; } }