nginx与apache一起做反向代理,各自的优点就不细说了,反正一个处理并发及静态的牛差差,一个处理动态的牛差差。
想看apache如何运行安装,可以查看我的另一篇centOS7 LAMP安装及注意要点。
1、apache相关配置文件更改端口号
主配置文件:
vi /etc/httpd/conf/httpd.conf
Listen 81
虚拟主机配置文件:
vi /etc/httpd/conf.d/lock.com.conf
<VirtualHost 192.168.136.128:81>
DirectoryIndex index.php
ServerAdmin 2871903572@qq.com
DocumentRoot /www/lockcom
ServerName lock.com
ServerAlias lock.com
<Directory /www/lockcom>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
打开浏览器
http://lock.com:81
应该可以访问。
2、nginx安装
用yum来安装吧
yum nginx install
/bin/systemctl start nginx.service
systemctl status nginx.service
打开浏览器
http://lock.com
应该是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
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
#listen 80 default_server;
#listen [::]:80 default_server;
listen 80;
server_name _;
#root /usr/share/nginx/html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
location / {
try_files $uri @apache;
}
location @apache {
internal;
proxy_pass http://192.168.136.128:81;
}
location ~ .*\.(php|php5)?$ {
proxy_pass http://192.168.136.128:81;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 7d;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
include /etc/nginx/vhost/*.conf;
}
虚拟主机下的配置vhost/lock.com.conf
vi /etc/nginx/vhost/lock.com.conf
server {
listen 80;
server_name lock.com;
access_log /www/wwwlogs/lock.com_nginx.log combined;
index index.html index.htm index.jsp index.php;
root /www/lockcom;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
if ( $http_user_agent ~ ApacheBench|WebBench|Jmeter|must-revalidate|Havij ){
return 503;
}
location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$ {
valid_referers none blocked nahehuo.com *.nahehuo.com;
if ($invalid_referer) {
#rewrite ^/ http://www.lock.com/403.html;
return 403;
}
}
limit_rate 500k;
location / {
try_files $uri @apache;
}
location @apache {
internal;
proxy_pass http://192.168.136.128:81;
}
location ~ .*\.(php|php5)?$ {
proxy_pass http://192.168.136.128:81;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 7d;
}
}