|
( c& K8 T& r0 E. X: @- B) D nginx的多域http、https同时访问配置及http重定向https1、关于ssl 服务证书的申请或生成就略过,nginx安装略过了解nginx配置的几个细节:(1)nginx的配置都是由 directives组成,directives由简单指令或者区块指令组成
3 `/ c2 s9 o; L/ X 简单指令:listen 80;区块指令由{}包含,区块指令又可以包含多个简单指令和区块指令:http {server {}}(2)关于端口映射访问同一nginx服务器,指向不同域,所以必须分配不同端口,如果用http://ip:port形式 ,会很不方便,所以需要用到端口映射,如下(www.aaa.com:8880、www.bbb.com:8881均指向80端口):。
" W. l: O) q; Q/ o server{listen 80;server_name www.aaa.com;location / {#....proxy_pass http://localhost:8880;}}server{listen 80; server_name www.bbb.com;location / {#....proxy_pass http://localhost:8881;}}(3)每次更改conf相关配置文件后需要重启nginx (4)特定跳转页面设置:不带www也能正常跳转,增加一个server如下:server{listen 80;server_name aaa.com;location / {#....proxy_pass http://localhost:8880; }...}或者进行301跳转server{listen 80;server_name aaa.com;rewrite ^/(.*) http://www.aaa.com/$1 permanent;}添加404网页,直接在里面添加,如: server{listen 80;server_name www.bbb.com; #绑定域名error_page 404 /404.html;}最后还有一个方法需要注意,需要禁止IP直接访问80端口或者禁止非本站的域名绑定我们的IP,如下处理,放到最前一个server上面即可: server{listen 80 default;servername ;return 403;}(5)每个域名可以写一个.conf文件,然后用include .conf导入配置,如下aaa.conf中的内容是: server{listen 80;server_name www.aaa.com;location / {#....proxy_pass http://localhost:8880;}... }aaa.conf都放在/data/nginx/conf/vhost目录下,然后在nginx.conf中使用引入命令: include /data/nginx/conf/vhost/*.conf;需要注意的是这句命令应该放在http{}的花括号内,因为include的命令引入相当于被引入的所有代码写在nginx.conf中一样。 2、nginx关于多域名访问服务器(1)配置nginx中conf文件夹下的nginx.conf加入代码(环境是windows 2008 server+upupw_np7.0)include vhosts.conf; (2)conf文件夹下新建vhost.conf, 加入以下内容:server {listen 80;server_name aaa.com www.aaa.com;location / {root C:/UPUPW_NP7.0/htdocs; index index.html index.htm default.html default.htm index.php default.php app.php u.php;include C:/UPUPW_NP7.0/htdocs/up- .conf;}autoindex off;include advanced_settings.conf;#include expires.conf;location ~ .\/(attachment|attachments|uploadfiles|avatar)\/. .(php|php5|phps|asp|aspx|jsp)$ {deny all;}location ~ ^.+.php {root C:/UPUPW_NP7.0/htdocs;fastcgi_pass bakend; fastcgi_index index.php;fastcgi_split_path_info ^((?U).+.php)(/?.+)$;fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;include fastcgi.conf;}}#反向代理到本机其他域名增加以下内容 server {listen 80; server_name bbb.com www.bbb.com;
' f7 O1 l, Q: G& Y location / {
]7 @8 A& A0 E( O* n proxy_pass http://127.0.0.1:8888/; #指定本机服务器其他端口,通过http://ip:port能访问到你的网站
! P' x- I9 v3 D: B include uproxy.conf;3 [5 _6 C l8 n3 U
}
: b4 q% L! ?5 |& H7 o8 E# k; @ }
: S L, d a3 N0 n& X- j( b 配置后可以同时访问aaa.com, bbb.com3、如果要http、https同时访问配置如下:server {listen 80;listen 443 ssl; server_name aaa.com www.aaa.com;
* B- L! K. x0 W#ssl on; #如果不取消本行会产生错误
! c- Z; n, N% z( \) _4 wssl_certificate C:/UPUPW_NP7.0/Nginx/cert/214534906590602.pem;
1 `" u0 L) ^$ v. g1 e: N9 }ssl_certificate_key C:/UPUPW_NP7.0/Nginx/cert/214534906590602.key; , J5 N! S& c1 Q( z+ ?6 _: U
#这里我使用的是阿里云的免费证书* f! R3 u3 f- |: {! u1 b
ssl_session_timeout 5m;4 ]+ r9 L: y3 D5 [* i
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;" ^5 U9 p" E" }+ F
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
8 l% J5 m2 N3 H4 @6 F: Dssl_prefer_server_ciphers on;
6 L7 E# }, m5 ]5 N6 A( [ location / {$ S: ]+ P n2 v5 J/ k" h! a# r) E
root C:/UPUPW_NP7.0/htdocs;4 E% G1 K( c; Y; ~( o( i
index index.html index.htm default.html default.htm index.php default.php app.php u.php;
; G/ l& G8 f! v include C:/UPUPW_NP7.0/htdocs/up-*.conf;3 C" x9 `$ K$ l j7 ^) `
}
& ^- K- Q, M: T1 @ autoindex off;
* x8 m6 e4 ?9 F/ D include advanced_settings.conf;$ ~% c7 U% E- j. X U6 s" e
#include expires.conf;
. C& z2 L( G* |; |4 `' T( ? location ~* .*\/(attachment|attachments|uploadfiles|avatar)\/.*\.(php|php5|phps|asp|aspx|jsp)$ {
: ]% A0 G# Z* P- W deny all;; p% P6 o& V7 C2 `, Z
}
5 Z; P) s1 N! x) o' p" n' f location ~ ^.+\.php {6 U) C( U \/ i5 |
root C:/UPUPW_NP7.0/htdocs;
# I/ @1 t7 [$ \0 G' f9 S fastcgi_pass bakend;
, u+ o' T( ^$ L$ d% G, w" O+ o fastcgi_index index.php;, m2 l$ R) W; @8 M4 J
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;' M) u8 j7 b, P- r3 W/ N3 u
fastcgi_param PATH_INFO $fastcgi_path_info;% [/ t: R4 V4 r( L( \# L
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;7 y3 ?$ c( S/ `7 H' U
include fastcgi.conf;; F0 e; c: W; R t/ c& o ?3 V" k
}
' M+ c t# C! | }4 A. R* @6 D4 N6 P6 l: E3 U2 y
#反向代理到本机其他域名增加以下内容server {listen 80; server_name bbb.com www.bbb.com;#ssl on;ssl_certificate C:/UPUPW_NP7.0/Nginx/cert/214543350020602.pem; ssl_certificate_key C:/UPUPW_NP7.0/Nginx/cert/214543350020602.key;ssl_session_timeout 5m;ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;ssl_prefer_server_ciphers on; location / {
9 q l' Z2 d' l# Z) ~ proxy_pass http://127.0.0.1:8888/; #指定本机服务器其他端口,通过http://ip:port能访问到你的网站/ J' l. Z! A- M
include uproxy.conf;$ s" n$ Z# p7 c6 G0 f
}
& W! e/ r' E: J/ U: J% [" m- f' J& p }3 h# |2 p- H6 o" i
**在设置443端口的时候遇到以下问题:nginx端口占用,启动报错:bind() to 0.0.0.0:443 failed (10013: An attempt was made to access a socket in a way f 解决方法:1)cmd输入netstat -aon | findstr “443” 查找端口占用情况,找到提示占用的端口号0.0.0.0:443,查看后,pid值为4, 在系统进程服务中查到pid=4的进程为一个系统后台服务 2)一般该服务为:Routing and Remote Access服务,只需在组件服务中把对应的停掉,重启nginx即可4、如果要让Http 重定向至 Https,对vhosts.conf配置如下:server{ listen 80;server_name aaaa.comm www.aaa.com;add_header Strict-Transport-Security max-age=15768000;return 301 https://$server_name$request_uri;! p4 X8 T/ S0 v1 W' m d
}server {#listen 80;listen 443 ssl; server_name aaa.com www.aaa.com;
; f1 ]6 Z! g8 y9 I Assl on; #如果不取消本行会产生错误8 _" k+ |1 D6 e' U' B! R" ]
ssl_certificate C:/UPUPW_NP7.0/Nginx/cert/214534906590602.pem;, A- j' T' _' i) _* F# r
ssl_certificate_key C:/UPUPW_NP7.0/Nginx/cert/214534906590602.key; # [+ r) x1 u, ^1 ^ K
#这里我使用的是阿里云的免费证书& ^% o: D3 |" R% X! h0 l Q" s: s
ssl_session_timeout 5m;3 m+ F( b: X7 P7 X' C
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
3 @' ^+ c2 f- i- vssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;* O! L- f! M/ X
ssl_prefer_server_ciphers on;5 \; j# d0 d* l; [
location / { T0 h3 a% |0 f$ G1 a# B
root C:/UPUPW_NP7.0/htdocs;" n: y+ W1 A' P j$ h& G+ \$ }
index index.html index.htm default.html default.htm index.php default.php app.php u.php;
: ?( i) \$ A; x( H7 t$ ]) C include C:/UPUPW_NP7.0/htdocs/up-*.conf;5 E2 l7 B1 N$ ^- m8 D9 m
}
1 t7 y8 g% y8 P( K1 I5 A. r, a1 p* ? autoindex off;
2 ~: r3 Z' G* l% }2 \ U8 Y include advanced_settings.conf;
) h/ _9 l7 t P/ f+ Z8 n #include expires.conf;
2 k& Z% A" n l, ]: `* R location ~* .*\/(attachment|attachments|uploadfiles|avatar)\/.*\.(php|php5|phps|asp|aspx|jsp)$ {" N* Y9 Z2 ^/ x+ I$ J' r
deny all;
- h% C- B* W5 w }
. E, O; t# T, e$ X# h location ~ ^.+\.php {; y" c; F {6 k( R0 x r, A
root C:/UPUPW_NP7.0/htdocs;+ P$ S# x% M. R& z# @! I- B# i+ ~1 o
fastcgi_pass bakend;5 l4 R4 U4 |& e- q8 N, {7 R
fastcgi_index index.php;
' u$ z s7 ]: P0 Q+ z6 J# p% k3 Z fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;% H# ` [* P! d" v3 O; ?
fastcgi_param PATH_INFO $fastcgi_path_info;8 A1 a, G) ^7 U1 ?( O
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
( N9 T! E4 {. C% W. s, N0 l- \) w include fastcgi.conf;* j: S- ^: H: |9 L7 l0 M" E. i
}: B2 Z1 n# b3 o7 ~9 n: `& V
}1 @. Q/ H1 x, u
#反向代理到本机其他域名增加以下内容server{listen 80;server_name bbb.com www.bbb.com;add_header Strict-Transport-Security max-age=15768000; return 301 https://$server_name$request_uri;}server {#listen 80; server_name bbb.com www.bbb.com;#ssl on; ssl_certificate C:/UPUPW_NP7.0/Nginx/cert/214543350020602.pem;ssl_certificate_key C:/UPUPW_NP7.0/Nginx/cert/214543350020602.key; ssl_session_timeout 5m;ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; location / {
7 N, y1 Y5 h0 h proxy_pass http://127.0.0.1:8888/; #指定本机服务器其他端口,通过http://ip:port能访问到你的网站8 b8 }. P0 l1 q, h: r
include uproxy.conf;
?3 J+ F0 j/ i1 Z0 ?* X }
9 D8 l5 l% `' p0 a! `7 t( Z; g' B }7 ~6 D) J3 E+ N) Z E
作者daydaydream的原创作品,如需转载,请注明出处,否则将追究法律责任转载地址:https://blog.51cto.com/13238147/2087756 |