apache迁移至nginx实践(虚拟主机配置)

注:用户信息,用大写字母X替换了

1、apache的httpd.conf 的部分配置文件(被替换对象)


RemoteIPHeader X-Real-IP
RemoteIPInternalProxy 127.0.0.1

 
          Options FollowSymlinks  
          AllowOverride All  
          Require all granted

#    DocumentRoot /data/user_XXXXXXX/weihu
#    DirectoryIndex weihu.html
    
    DocumentRoot /data/user_XXXXXXX/www/www_html

    ServerName www.XXXXXX.com
    ProxyPass /service_XXXXXXX http://www.XXXXXX.com:9080
    ProxyPassReverse /service_XXXXXXX http://www.XXXXXX.com:9080
    ProxyPass /webservice http://www.XXXXXX.com:9088/webservice
    ProxyPassReverse /webservice http://www.XXXXXX.com:9088/webservice
        ProxyPass /ws http://www.XXXXXX.com:9088/ws
    ProxyPassReverse /ws http://www.XXXXXX.com:9088/ws
        ProxyPass /wxssl http://www.XXXXXX.com:9088/wxssl
    ProxyPassReverse /wxssl http://www.XXXXXX.com:9088/wxssl
   # ErrorLog logs/www.XXXXXX.com-error_log
   # CustomLog logs/www.XXXXXX.com-access_log common
    ErrorLog "|/home/user_XXXXXXX/software/cronolog/sbin/cronolog /home/user_XXXXXXX/software/apache24/logs/log/www.XXXXXX.com-error_%Y%m%d.log"
    CustomLog "|/home/user_XXXXXXX/software/cronolog/sbin/cronolog /home/user_XXXXXXX/software/apache24/logs/log/www.XXXXXX.com-access_%Y%m%d.log" common

    SetEnv force-proxy-request-1.0 1
    SetEnv proxy-nokeepalive 1
    ProxyTimeout 1200
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^XXXXXX.com [NC]
    RewriteRule ^(.*) http://www.XXXXXX.com/ [L]

    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
    RewriteRule .* - [F]

    ErrorDocument 403 http://www.XXXXXX.com/error.html
    ErrorDocument 404 http://www.XXXXXX.com/error.html
    ErrorDocument 405 http://www.XXXXXX.com/error.html
    ErrorDocument 500 http://www.XXXXXX.com/error.html



    ServerName upload.XXXXXX.com
    DocumentRoot /data/user_XXXXXXX/www/uploadfiles
    #ErrorLog logs/upload-error_log
    #CustomLog logs/upload-access_log common
    ErrorLog "|/home/user_XXXXXXX/software/cronolog/sbin/cronolog /home/user_XXXXXXX/software/apache24/logs/log/upload-error_%Y%m%d.log"
    CustomLog "|/home/user_XXXXXXX/software/cronolog/sbin/cronolog /home/user_XXXXXXX/software/apache24/logs/log/upload-access_%Y%m%d.log" common

    SetEnv force-proxy-request-1.0 1
    SetEnv proxy-nokeepalive 1
    ProxyTimeout 1200
    ErrorDocument 403 http://www.XXXXXX.com/error.html
    ErrorDocument 404 http://www.XXXXXX.com/error.html
    ErrorDocument 405 http://www.XXXXXX.com/error.html
    ErrorDocument 500 http://www.XXXXXX.com/error.html

    RewriteEngine on
    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
    RewriteRule .* - [F]



    DocumentRoot /data/user_XXXXXXX/www/operation_html
    ServerName op.XXXXXX.com
    ProxyPass /service_XXXXXXX http://www.XXXXXX.com:9080
    ProxyPassReverse /service_XXXXXXX http://www.XXXXXX.com:9080
#    ErrorLog logs/op000-error_log
#    CustomLog logs/op000-access_log common
    ErrorLog "|/home/user_XXXXXXX/software/cronolog/sbin/cronolog /home/user_XXXXXXX/software/apache24/logs/log/op000-error_%Y%m%d.log"
    CustomLog "|/home/user_XXXXXXX/software/cronolog/sbin/cronolog /home/user_XXXXXXX/software/apache24/logs/log/op000-access_%Y%m%d.log" common

    SetEnv force-proxy-request-1.0 1
    SetEnv proxy-nokeepalive 1
    ProxyTimeout 1200

    RewriteEngine on
    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
    RewriteRule .* - [F]


    DocumentRoot /data/user_XXXXXXX/www/mobile_html
    ServerName mobile.XXXXXX.com
    DirectoryIndex default.html
    ProxyPass /service_XXXXXXX http://www.XXXXXX.com:9080
    ProxyPassReverse /service_XXXXXXX http://www.XXXXXX.com:9080
#    ErrorLog logs/mobile000-error_log
#    CustomLog logs/mobile000-access_log common
    ErrorLog "|/home/user_XXXXXXX/software/cronolog/sbin/cronolog /home/user_XXXXXXX/software/apache24/logs/log/mobile000-error_%Y%m%d.log"
    CustomLog "|/home/user_XXXXXXX/software/cronolog/sbin/cronolog /home/user_XXXXXXX/software/apache24/logs/log/mobile000-access_%Y%m%d.log" common

    SetEnv force-proxy-request-1.0 1
    SetEnv proxy-nokeepalive 1
    ProxyTimeout 1200

    RewriteEngine on
    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
    RewriteRule .* - [F]


       
                header set cache-control "no-cache"
       

TraceEnable off

2、nginx的安装

2.1 下载nginx、pcre并解压
drwxr-xr-x  9 user_XXXXXXX users      4096  5月 22 21:44 nginx-1.16.0
-rw-r--r--  1 user_XXXXXXX users   1032345  5月 17 11:32 nginx-1.16.0.tar.gz
drwxr-xr-x  9 user_XXXXXXX users     12288  5月 22 21:45 pcre-8.43
-rw-r--r--  1 user_XXXXXXX users   2085854  5月 20 15:20 pcre-8.43.tar.gz

2.2 cd nginx-1.16.0

./configure --prefix=/home/user_XXXXXXX/software/nginx  --with-pcre=/home/user_XXXXXXX/software/pcre-8.43

make && make install

2.2 安装后的目录

drwxr-xr-x 11 user_XXXXXXX users      4096  5月 22 22:14 nginx

2.3 nginx的nginx.conf 的配置文件

user root;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;

events {
    use epoll;
    worker_connections  65535;
}


http {

    #关闭显示nginx版本号,安全要求
    server_tokens off;
    include       mime.types;
    default_type  application/octet-stream;

    #设置日志格式
    log_format  main  '$remote_addr - $remote_user [$time_iso8601] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

   #增加这个参数就可以指定最大上传文件大小为10m了;否则会报:状态码: 413 / Request Entity Too Large

   client_max_body_size 10m;  

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


    #www.XXXXXX.com
    server {  
        listen       80;  
        server_name  www.XXXXXX.com;

        if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') {
                set $tttt $1;
        }
        
        #配置日志,按天分割
        access_log  logs/www.XXXXXX.com-access-$tttt.log  main;

        #
        if ($request_method !~* GET|POST) {
           return 403;
        }
  
        location ~ .*\.(jpg|gif|jpeg|bmp|png|ico|json)$   
        {              
            root /data/user_XXXXXXX/www/www_html/;  
            expires      7d;
        }   
  
        location /service_XXXXXXX/ { 
                        limit_except GET POST {deny  all; } 
                        proxy_set_header Host www.XXXXXX.com;
                        proxy_set_header X-real-ip $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_pass http://www.XXXXXX.com:9080/;
        }
        location /webservice/ {  
                        limit_except GET POST {deny  all; } 
                        proxy_set_header Host www.XXXXXX.com;
                        proxy_set_header X-real-ip $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_pass http://www.XXXXXX.com:9088/webservice/;
        }
        location /ws/ {
                        limit_except GET POST {deny  all; } 
                        proxy_set_header Host www.XXXXXX.com;
                        proxy_set_header X-real-ip $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_pass http://www.XXXXXX.com:9088/ws/;
        }
        location /wxssl/ {
                        limit_except GET POST {deny  all; } 
                        proxy_set_header Host www.XXXXXX.com;
                        proxy_set_header X-real-ip $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_pass http://www.XXXXXX.com:9088/wxssl/;
        }
        location / {
                        limit_except GET POST {deny  all; } 
            root /data/user_XXXXXXX/www/www_html/;  
        }    
        error_page   403 404 500 502 503 504  /error.html;
        location = /error.html {
                    limit_except GET POST {deny  all; } 
            root   /data/user_XXXXXXX/www/www_html/;
        }
    }

    #upload.XXXXXX.com
    server {  
        listen       80;  
        server_name  upload.XXXXXX.com;

        if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') {
                set $tttt $1;
        }

        access_log  logs/upload-access-$tttt.log  main;

        if ($request_method !~* GET|POST) {
           return 403;
        }  

        location ~ .*\.(jpg|gif|jpeg|bmp|png|ico|json)$   
        {              
            root /data/user_XXXXXXX/www/uploadfiles/;  
            expires      7d;
        }   
  
        location / {
            limit_except GET POST {deny  all; } 
            root /data/user_XXXXXXX/www/uploadfiles/;  
        }    
        error_page   403 404 500 502 503 504  /error.html;
        location = /error.html {
                    limit_except GET POST {deny  all; } 
            root   /data/user_XXXXXXX/www/www_html/;
        }
    }

    #op.XXXXXX.com
    server {  
        listen       80;  
        server_name  op.XXXXXX.com;

        if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') {
                set $tttt $1;
        }
        access_log  logs/op-access-$tttt.log  main;

        if ($request_method !~* GET|POST) {
           return 403;
        }  

        location ~ .*\.(jpg|gif|jpeg|bmp|png|ico|json)$   
        {              
            root /data/user_XXXXXXX/www/operation_html/;  
            expires      7d;
        }   
  
        location /service_XXXXXXX/ { 
                        limit_except GET POST {deny  all; } 
                        proxy_set_header Host www.XXXXXX.com;
                        proxy_set_header X-real-ip $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_pass http://www.XXXXXX.com:9080/;
        }
        location / {
            limit_except GET POST {deny  all; } 
            root /data/user_XXXXXXX/www/operation_html/;  
        }    
        error_page   403 404 500 502 503 504  /error.html;
        location = /error.html {
                    limit_except GET POST {deny  all; } 
            root   /data/user_XXXXXXX/www/www_html/;
        }
    }

    #mobile.XXXXXX.com
    server {  
        listen       80;  
        server_name  mobile.XXXXXX.com;

        if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') {
                set $tttt $1;
        }
        access_log  logs/mobile-access-$tttt.log  main;
  
        if ($request_method !~* GET|POST) {
           return 403;
        }

        location ~ .*\.(jpg|gif|jpeg|bmp|png|ico|json)$   
        {              
            root /data/user_XXXXXXX/www/mobile_html/;  
            expires      7d;
        }
  
        location /service_XXXXXXX/ { 
                        limit_except GET POST {deny  all; } 
                        proxy_set_header Host www.XXXXXX.com;
                        proxy_set_header X-real-ip $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_pass http://www.XXXXXX.com:9080/;
        }
        location / {
            limit_except GET POST {deny  all; } 
            root /data/user_XXXXXXX/www/mobile_html/;  
        }    
        error_page   403 404 500 502 503 504  /error.html;
        location = /error.html {
            limit_except GET POST {deny  all; } 
            root   /data/user_XXXXXXX/www/www_html/;
        }
    }
}

2.4 nginx的启动

由于使用的是80端口,必须用root身份启动,所以采用添加sudo权限,给普通用户授权

切换到root,编辑 /etc/sudoers  

# User privilege specification
root ALL=(ALL) ALL
user_XXXXXXX  ALL=NOPASSWD: /home/user_XXXXXXX/software/nginx/sbin/nginx

#备注,下面这句,可能不用加,没有验证,以前留下的。
user_XXXXXXX  ALL=/usr/sbin/useradd, /usr/sbin/userdel, /usr/sbin/usermod, /usr/bin/chage, /usr/bin/passwd, !/usr/sbin/useradd *-o*, !/usr/sbin/userdel *root*, !/usr/sbin/usermod *-o*, !/usr/bin/chage *root*, !/usr/bin/passwd *root*

启动:sudo ./nginx

停止:sudo ./nginx -s stop

修改了ngxin.conf,重新加载配置文件:sudo ./nginx -s reload

2.5 做个shell脚本,方便启停nginx

#!/bin/bash
nginxbase=/home/user_XXXXXXX/software/nginx

start()
{
  cd ${nginxbase%/}/sbin
  sudo ./nginx
  echo "nginx started!"
}

stop()
{
  cd ${nginxbase%/}/sbin
  sudo ./nginx -s stop
  echo "nginx stopped!"
}

restart()
{
  cd ${nginxbase%/}/sbin
  sudo ./nginx -s reload
  echo "nginx restarted!"
}


case $1 in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  *)
    echo "Please usage:$0 {start|stop|restart}"
    ;;
esac

你可能感兴趣的:(NGINX,nginx,虚拟主机,apache)