Windows下用Nginx配置https服務(wù)器及反向代理的問(wèn)題
請(qǐng)求邏輯
前端 --> https方式請(qǐng)求nginx
nginx --> 通過(guò)http請(qǐng)求后端服務(wù)
安裝OpenSSL
然后配置環(huán)境變量。在系統(tǒng)環(huán)境變量中添加環(huán)境變量:
變量名:OPENSSL_HOME
變量值:F:\OpenSSL-Win64\bin;
(變量值為OPENSSL安裝位置下的bin目錄)
生成證書(shū)
用命令行隨便打開(kāi)一個(gè)目錄, 使用如下命令生成證書(shū)
# 創(chuàng)建私鑰 # test文件名是自己隨便起即可, 這個(gè)命令會(huì)讓你設(shè)置兩次rsa的密碼, 請(qǐng)務(wù)必記住該密碼, 后續(xù)需要使用, 命令執(zhí)行完畢, 會(huì)在當(dāng)前目錄生成 test.key 的文件 openssl genrsa -des3 -out test.key 1024 # 創(chuàng)建csr證書(shū), 這里用到的 test.key 是上一個(gè)命令生成的那個(gè). 執(zhí)行這個(gè)命令后,需要輸入一系列的信息。輸入的信息中最重要的為Common Name,這里輸入的域名即為我們要使用https訪問(wèn)的域名 ,比如我輸入的是localhost。其它的內(nèi)容隨便填即可。以上步驟完成后,ssl文件夾內(nèi)出現(xiàn)兩個(gè)文件:test.csr 和 test.key openssl req -new -key test.key -out test.csr # 去除密碼 # 在加載SSL支持的Nginx并使用上述私鑰時(shí)除去必須的口令,否則會(huì)在啟動(dòng)nginx的時(shí)候需要輸入密碼。 # 復(fù)制test.key并重命名為test.copy.key # 在命令行中執(zhí)行如下命令以去除口令(此時(shí)需要輸入密碼,這個(gè)密碼就是上文中在創(chuàng)建私鑰的時(shí)候輸入的密碼。) openssl rsa -in test.copy.key -out test.key # 生成crt證書(shū). 證書(shū)生成完畢。我們發(fā)現(xiàn),ssl文件夾中一共生成了4個(gè)文件。下面,配置https服務(wù)器的時(shí)候,我們需要用到的是其中的test.crt和test.key這兩個(gè)文件。 openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt
下載安裝nginx, 修改nginx配置
將生成的test.key
和 test.crt
移動(dòng)到 $NGINX_ROOT/conf
目錄
#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 localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root D:/local-site; 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; #} } server { listen 8086; listen localhost:8086; server_name localhost; gzip on; gzip_buffers 4 16k; gzip_comp_level 6; gzip_vary on; gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript; location / { root D:/local-site/good-test; index index.html index.htm; } location ^~/api/ { rewrite ^~/api/(.*)$ /$1 break; proxy_pass http://localhost:8080/; #代理IP:端口 } } # HTTPS server 配置, 這里使用了反向代理和跨域支持, 注意nginx和后端服務(wù), 只需要在nginx設(shè)置跨域即可, 后端服務(wù)的跨域不要開(kāi)啟, 如果兩邊都開(kāi)啟了跨域, 會(huì)出問(wèn)題 # server { listen 443 ssl; server_name localhost; ssl_certificate test.crt; ssl_certificate_key test.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } location / { # rewrite ^~/api/(.*)$ /$1 break; # add_header Access-Control-Allow-Origin *; # 允許客戶(hù)端的請(qǐng)求方法 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT'; # 允許客戶(hù)端提交的的請(qǐng)求頭 add_header 'Access-Control-Allow-Headers' 'Origin, x-requested-with, Content-Type, Accept, Authorization'; # 允許客戶(hù)端提交Cookie add_header 'Access-Control-Allow-Credentials' 'true'; # 允許客戶(hù)端訪問(wèn)的響應(yīng)頭 add_header 'Access-Control-Expose-Headers' 'Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma'; proxy_pass http://10.114.119.61: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; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } server { listen 8443 ssl; server_name localhost; ssl_certificate test.crt; ssl_certificate_key test.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } location / { # rewrite ^~/api/(.*)$ /$1 break; # add_header Access-Control-Allow-Origin $http_origin; # 允許客戶(hù)端的請(qǐng)求方法 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT'; # 允許客戶(hù)端提交的的請(qǐng)求頭 add_header 'Access-Control-Allow-Headers' 'Origin, x-requested-with, Content-Type, Accept, Authorization'; # 允許客戶(hù)端提交Cookie add_header 'Access-Control-Allow-Credentials' 'true'; # 允許客戶(hù)端訪問(wèn)的響應(yīng)頭 add_header 'Access-Control-Expose-Headers' 'Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma'; # 這是是配置需要代理的服務(wù) proxy_pass http://10.114.119.61:7001; # proxy_pass https://172.16.46.38:8443; # proxy_pass http://10.114.119.61:8866; 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; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } }
重啟nginx
本地域名配置
打開(kāi)C:\Windows\System32\drivers\etc\hosts
文件
加入配置:
10.114.119.61 pan.test.com
10.114.119.61 pan.uat.com
到此這篇關(guān)于Windows下用Nginx配置https服務(wù)器及反向代理的文章就介紹到這了,更多相關(guān)Nginx配置https服務(wù)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx 多個(gè)location轉(zhuǎn)發(fā)任意請(qǐng)求或訪問(wèn)靜態(tài)資源文件的實(shí)現(xiàn)
這篇文章主要介紹了nginx 多個(gè)location轉(zhuǎn)發(fā)任意請(qǐng)求或訪問(wèn)靜態(tài)資源文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Nginx之location匹配和Rewrite重寫(xiě)跳轉(zhuǎn)方式
這篇文章主要介紹了Nginx之location匹配和Rewrite重寫(xiě)跳轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06使用Nginx解決跨域訪問(wèn)問(wèn)題的完整案例
在現(xiàn)代的Web開(kāi)發(fā)中,跨域訪問(wèn)是一種常見(jiàn)的需求,由于瀏覽器的同源策略,不同域名之間的訪問(wèn)存在一定的限制,本文將介紹如何使用Nginx來(lái)解決跨域訪問(wèn)的問(wèn)題,并通過(guò)一個(gè)完整的實(shí)例來(lái)展示,需要的朋友可以參考下2024-03-03Nginx+Tomcat搭建高性能負(fù)載均衡集群的實(shí)現(xiàn)方法
這篇文章主要介紹了Nginx+Tomcat搭建高性能負(fù)載均衡集群的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03