windows 下 Nginx(代理)+Tomcat(Java)+Apache(PHP)共用80端口

解決的核心問題是:使用一個80端口,根據域名、子域名或IP  同時訪問java + php運行環境

基本思想:將apache設置爲808端口,tomcat設置爲8080端口,Nginx設置爲80端口,主要是通過Nginx反向代理,通過分別配置的域名,映射到相應的端口對服務器進行訪問。

下載Nginx

http://nginx.org/download/nginx-1.16.1.zip

解壓後如下

2.nginx的運用

有很多種方法啓動nginx

(1)直接雙擊解壓文件夾裏的nginx.exe,雙擊後一個黑色的彈窗一閃而過,證明啓動成功

(2)打開cmd命令窗口,切換到nginx解壓目錄下,輸入命令 nginx.exe 或者 start nginx ,回車即可;

常用命令如下:

1. 快速停止或關閉:nginx -s stop

2. 正常停止或關閉:nginx -s quit

3. 配置文件修改重裝載命令:nginx -s reload

檢查nginx是否啓動成功:

cmd命令窗口輸入命令:tasklist /fi "imagename eq nginx.exe" 出現如下結果說明啓動成功

3.nginx代理設置

①修改要用到的配置文件

       ..\nginx-1.16.1\conf\nginx.conf

Java

upstream java{

         server 127.0.0.1:8080 weight=1;

     }

    server {
        listen       80;
        server_name  127.0.0.1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
             proxy_pass   http://java;

            proxy_pass_request_headers on;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

 

PHP

 upstream php{

              server 127.0.0.1:808 weight=1;

      }      

    server {

        listen       80;

        server_name  www.xuliwei.com;

              location / {

                     proxy_pass   http://php;

                     proxy_pass_request_headers on;
                     proxy_set_header X-Forwarded-Host $host;
                     proxy_set_header X-Forwarded-Server $host;
                     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                   

              }

       }

$_SERVER['HTTP_X_FORWARDED_FOR']就可以拿到真實IP

②Tomcat : ..\apache-tomcat-8.0\conf\server.xml

默認端口爲8080

 

③Apache: ..\Apache24\conf\httpd.conf

Listen 80 改 Listen 808

注意檢查下httpd-vhosts.conf下端口是否改成808

 

④Hosts : C:\Windows\System32\drivers\etc\HOSTS

增加一行數據

127.0.0.1 www.xuliwei.com

重新啓動Tomcat/Apache/nginx

 

最後訪問:

127.0.0.1 會進入 8080端口的Tomcat 環境

ww.xuliwei.com會進入 808 端口的 Apache 環境

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章