1. 程式人生 > >nginx配置ssl協議https +tomcat 非80/443埠反向代理的配置方式

nginx配置ssl協議https +tomcat 非80/443埠反向代理的配置方式

--開始

Nginx增加以下配置

proxy_set_header Host $host:$server_port; 非80埠 ,用80埠時 不需要$server_port

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Proto $scheme;

--結束

-----本人server示例 開始-----

nginx.conf

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
	
	include nixcraft.in.conf;
}

nixcraft.in.conf
upstream ndgweb {
    server 192.168.168.168:8037;
}
upstream gwapp{
    server 192.168.168.125:8101;
}

server {
    access_log  logs/access.log;
    error_log   logs/error.log;
    index       index.html;
    root        /usr/local/nginx/html;
    server_name www.j*s*****o.com;
    listen 8100 ssl;
    ssl on;
    ssl_certificate      ssl.cer;
    ssl_certificate_key  ssl.key;

    ## Only allow these request methods
    if ($request_method !~ ^(GET|HEAD|POST)$ ) {
        return 444;
    }
 
    ## PROXY - Web
    location / {
        proxy_pass  http://ndgweb;
        proxy_ignore_headers   Expires Cache-Control;
 
        proxy_set_header        Host            $host:$server_port;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;
    }

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
server {
    access_log  logs/access.log;
    error_log   logs/error.log;
    index       index.html;
    root        /usr/local/nginx/html;
    server_name d**o*e**n.g***.net;
    listen 8100 ssl;
    ssl on;
    ssl_certificate      ssl1.cer;
    ssl_certificate_key  ssl1.key;

    ## Only allow these request methods
    if ($request_method !~ ^(GET|HEAD|POST)$ ) {
        return 444;
    }
 
    ## PROXY - Web
    location / {
        proxy_pass  https://gwapp;
        proxy_ignore_headers   Expires Cache-Control;
 
        proxy_set_header        Host            $host:$server_port;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;
    }

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

-----本人server示例 結束-----

另附nginx常用命令

1)下載地址:
  http://nginx.org
2)啟動
  解壓至c:\nginx,執行nginx.exe(即nginx -c conf\nginx.conf),預設使用80埠,日誌見資料夾C:\nginx\logs
3)使用
  http://localhost
4)關閉
  nginx -s stop 或taskkill /F /IM nginx.exe > nul
5)常用配置
   C:\nginx\conf\nginx.conf,使用自己定義的conf檔案如my.conf,命令為nginx -c conf\my.conf
6)檢視nginx程序
  tasklist /fi "imagename eq nginx.exe",如下顯示:
7)nginx常用命令
nginx -s stop 強制關閉
nginx -s quit 安全關閉
nginx -t 檢測配置檔案的正確性
nginx -s reload 改變配置檔案的時候,重啟nginx工作程序,來時配置檔案生效
nginx -s reopen 開啟日誌檔案