安裝nginx參考本人另外一篇博客:http://www.cnblogs.com/gmq-sh/p/5750833.htmljavascript
spring-boot須要啓動nginx的,用於監聽啓動的端口。
1、配置nginx:html
#user nobody;
worker_processes 1;
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name testgmq.com; #域名
index myindex.html; #指定的server的root的訪問頁面
root /home/www/index; #指定的server的root目錄
#charset koi8-r;
#access_log logs/host.access.log main;
#我工程的context-path=mytest
location /mytest {
proxy_pass http://localhost:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
# root html;
# index index.html index.htm;
}
}
}
這裏有三點須要說明:
一、nginx容許一個server同時支持http和https兩種協議。這裏咱們分別定義了http:80和https:443兩個協議和端口號。若是你不須要http:80則可刪除那行。
二、nginx收到請求後將經過http協議轉發給tomcat。因爲nginx和tomcat在同一臺機裏所以nginx和tomcat之間無需使用https協議。
三、因爲對tomcat而言收到的是普通的http請求,所以當tomcat裏的應用發生轉向請求時將轉向爲http而非https,爲此咱們須要告訴tomcat已被https代理,方法是增長X-Forwared-Proto和X-Forwarded-Port兩個HTTP頭信息。java
2、接着再配置tomcat。基於spring-boot開發時只需在application.properties中進行配置:
server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto
server.tomcat.port-header=X-Forwarded-Port
server.use-forward-headers=true
該配置將指示tomcat從HTTP頭信息中去獲取協議信息(而非從HttpServletRequest中獲取),同時,若是你的應用還用到了spring-security則也無需再配置。
此外,因爲spring-boot足夠自動化,你也能夠把上面四行變爲兩行:
server.tomcat.protocol_header=x-forwarded-proto
server.use-forward-headers=true
下面這樣寫也能夠:
server.tomcat.remote_ip_header=x-forwarded-for
server.use-forward-headers=true
但不能只寫一行:
server.use-forward-headers=true
具體請參見http://docs.spring.io/spring-boot/docs/1.3.0.RELEASE/reference/htmlsingle/#howto-enable-https,其中說到:
server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto
The presence of either of those properties will switch on the valve
此外,雖然咱們的tomcat被nginx反向代理了,但仍可訪問到其8080端口。爲此可在application.properties中增長一行:
server.address=127.0.0.1
這樣一來其8080端口就只能被本機訪問了,其它機器訪問不到。nginx
我是springboot,加了context-path=myproject,可是我綁定的域名又想:www.myxxx.com直徑訪問我項目中的其中的一個url,因此:spring
在server_name下面增長了:index,root兩個配置。tomcat
/home/www/index/myindex.html:springboot
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> <!-- 特定的url做爲首頁 --> window.location.href = "http://www.myxxx.com/myproject/mobile/shopindex.html"; </script> </body> </html>
做爲跳轉的處理。app
若是有更好的方法,能夠互相交流。tcp