|
6 F1 A, h7 @; I3 }1 c x
nginx的多域http、https同时访问配置及http重定向https1、关于ssl 服务证书的申请或生成就略过,nginx安装略过了解nginx配置的几个细节:(1)nginx的配置都是由 directives组成,directives由简单指令或者区块指令组成
+ R" a! M# A' x! [3 K8 {4 F 简单指令:listen 80;区块指令由{}包含,区块指令又可以包含多个简单指令和区块指令:http {server {}}(2)关于端口映射访问同一nginx服务器,指向不同域,所以必须分配不同端口,如果用http://ip:port形式 ,会很不方便,所以需要用到端口映射,如下(www.aaa.com:8880、www.bbb.com:8881均指向80端口):。
$ c1 Q( V- U$ E0 [' z 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;2 J8 v2 k/ v& K( S g8 q, R
location / {! F1 \' v) f w D$ @9 p$ o P
proxy_pass http://127.0.0.1:8888/; #指定本机服务器其他端口,通过http://ip:port能访问到你的网站- x% l- c C0 E' P9 D, L8 u
include uproxy.conf;
1 q0 p6 X/ e8 Z: t6 n }3 j' Q5 W5 i( J) Y/ e# ^' ]
}: A: E9 b' u/ N V" _
配置后可以同时访问aaa.com, bbb.com3、如果要http、https同时访问配置如下:server {listen 80;listen 443 ssl; server_name aaa.com www.aaa.com;
4 y# o! P1 h' B#ssl on; #如果不取消本行会产生错误. i2 m+ @+ E2 {4 ?0 `, m) a
ssl_certificate C:/UPUPW_NP7.0/Nginx/cert/214534906590602.pem;
! D2 B4 X& V0 | _! i5 dssl_certificate_key C:/UPUPW_NP7.0/Nginx/cert/214534906590602.key; 9 [6 S# G" a! o& j
#这里我使用的是阿里云的免费证书
* [: d# ?( ]( X; Q( J) Fssl_session_timeout 5m;
# U6 h6 z; a# v" W) z+ \ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
! A7 G, I# M ]$ x' c. e" rssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
) z! x8 z- F2 E7 o5 n: Q$ cssl_prefer_server_ciphers on;& q6 j5 M2 f4 p
location / {
( n4 D* p) c( V, S9 x$ X% Q- Q root C:/UPUPW_NP7.0/htdocs;
7 A' E% l3 D9 w# i- Y: N6 G index index.html index.htm default.html default.htm index.php default.php app.php u.php;
2 v% E& O" r" n, l8 L include C:/UPUPW_NP7.0/htdocs/up-*.conf;: T+ }! M7 e: B9 ]% G
}9 o1 V9 z$ u7 r
autoindex off;
" c8 O+ ?8 z7 l6 [4 i include advanced_settings.conf;
/ n$ F, C0 f2 G+ w" y #include expires.conf;' a7 _8 X4 F4 E4 m
location ~* .*\/(attachment|attachments|uploadfiles|avatar)\/.*\.(php|php5|phps|asp|aspx|jsp)$ {
# |. ^6 q* m7 g7 l1 Y: Q deny all;3 s4 L! I3 {; ?, N
}
9 L" F/ j/ i, F) I' g% L9 }/ q3 d location ~ ^.+\.php {$ P9 Z9 F% H4 E% m& h/ F
root C:/UPUPW_NP7.0/htdocs;( i# [( S2 g) m! a
fastcgi_pass bakend;! r% m5 {4 v0 @& c
fastcgi_index index.php;5 u" q6 Y$ H3 `* }
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;! W. Q1 ]2 Y+ B0 E
fastcgi_param PATH_INFO $fastcgi_path_info;
* m5 D7 n( B) R4 ~4 B, u fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
# }1 _) ]& K; U8 T3 H7 Q2 J3 o include fastcgi.conf;
* J2 ~/ V) E" M. d6 H }
; R& A& u) `6 [& h- i }
2 W6 \% W6 b' x$ E8 _! K #反向代理到本机其他域名增加以下内容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 / {
6 ~1 r, b, v( W, s- B proxy_pass http://127.0.0.1:8888/; #指定本机服务器其他端口,通过http://ip:port能访问到你的网站" |; x& P8 A5 B
include uproxy.conf;0 J3 R% A% E2 k0 q
}
7 G6 m$ G s# K }
4 X0 E: x" ?+ ~ **在设置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;
' Q4 y3 H% y, H. u }server {#listen 80;listen 443 ssl; server_name aaa.com www.aaa.com;. N l" Q% R. J7 d
ssl on; #如果不取消本行会产生错误
, x$ S/ y: d/ e" `4 l( Z$ O1 ussl_certificate C:/UPUPW_NP7.0/Nginx/cert/214534906590602.pem;
5 k- q; @! N% _: V$ c& sssl_certificate_key C:/UPUPW_NP7.0/Nginx/cert/214534906590602.key;
4 F- `! E% e) j8 h#这里我使用的是阿里云的免费证书
' ?3 ^) d! m/ t% |ssl_session_timeout 5m;! C1 X: y# v/ i# r4 X: ~0 N% V( ?
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;% X+ ^1 [) f( z* m. w
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;# w% A# ~( r. e! W' g
ssl_prefer_server_ciphers on;
, U9 N% T; w2 M# _$ L( N! |3 L location / {
# e1 A* U* }$ L! b0 W% Q root C:/UPUPW_NP7.0/htdocs;2 j) Z5 D+ a, P7 |, e3 E+ J z0 s
index index.html index.htm default.html default.htm index.php default.php app.php u.php;
( t) o% c: r. P _/ l- ` include C:/UPUPW_NP7.0/htdocs/up-*.conf;) l5 H/ @/ \& e1 n1 R
}
( D% P/ ]) f) ^: z: P autoindex off;
9 d% y% B6 k% ^% j( u include advanced_settings.conf;) N8 q9 s" y8 Y$ J4 q
#include expires.conf;
4 ]; Y/ v% i! J location ~* .*\/(attachment|attachments|uploadfiles|avatar)\/.*\.(php|php5|phps|asp|aspx|jsp)$ {
$ l/ L. L6 ]) D deny all;
) R0 R/ n7 n& G. s% _) m }4 @4 h1 ~! U' J* R. h# ?1 I
location ~ ^.+\.php {' Q" K+ Y' n: Q5 }- C, H
root C:/UPUPW_NP7.0/htdocs;8 _4 M, e1 O- u
fastcgi_pass bakend; Z; ]% o: Y# }) M9 F
fastcgi_index index.php;2 T+ ?# {( ]! n6 ?. R
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;" N+ W5 X, D- l+ j2 p- d0 H7 Q
fastcgi_param PATH_INFO $fastcgi_path_info;! |. p P' b& S' f- V- C( R6 u
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
- S$ ?4 d& d/ q* ~$ l' L( ?) U include fastcgi.conf;& V/ s; i5 X9 ?) s8 T
}% j+ [( i$ i. @ q* W k+ _
}
, J# @: }0 Y+ b+ z4 M #反向代理到本机其他域名增加以下内容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 / {
9 F$ [2 d( J0 x3 s! v. n. S$ l/ w proxy_pass http://127.0.0.1:8888/; #指定本机服务器其他端口,通过http://ip:port能访问到你的网站
0 N0 K# m a D include uproxy.conf;
. `0 O$ q7 x% z, K4 D) p# l }
& w, Q- b3 h9 f% I }
* V( M6 ^3 t% G+ P 作者daydaydream的原创作品,如需转载,请注明出处,否则将追究法律责任转载地址:https://blog.51cto.com/13238147/2087756 |