Nginx即可以做为反向代理服务,也可以做Web服务。入门一中,已经尝鲜了一下。只不过入门一里面主要应用了Nginxr的负载均衡。下面则是Nginx作为Web服务器的应用:
#user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types;# 表示Nginx 支持的MIME类型。 default_type application/octet-stream; sendfile on; keepalive_timeout 65;# 表示超时时长。 gzip on;# 启用gzip压缩。 server { listen 89; # 表示Nginx监听的端口号。 server_name localhost; # 表示Nginx Web服务器的主机名。 location / { root wwwroot;# 表示Web服务器的根目录路径,可以是相对路径也可以是绝对路径。相对路径主要是指相对于Nginx的运行环境的目录。 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;# 表示出错后跳转到错误页的根目录,可以是相对路径也可以是绝对路径。相对路径主要是指相对于Nginx的运行环境的目录。 } } }
另外,如果让Nginx作为编译型语言的Web服务器就需要一些第三方组件的支持了,参见张善友先生的博客,Ctrl+F,搜索Nginx可以看到相关的文章。后面会涉及到。
Nginx作为Web服务器一般都是做为静态内容的站点。
本人学识浅薄,只能一点儿一点儿深入。这也是我学习的记录,或许以后会用到。
布局文件:res/layout/activity_my.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" tools:context=".MyActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/wo" android:textColorHint="@color/calamus" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/shi" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/hao" android:textColor="@color/calamus" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/ren" /> </LinearLayout>
资源文件:res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Linear</string> <string name="action_settings">Settings</string> <string name="wo">我</string> <string name="shi">是</string> <string name="hao">好</string> <string name="ren">人</string> </resources>
资源文件:res/values/styles.xml
<resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="android:Theme.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> <color name="calamus">#c77eb5</color> </resources>
资源索引文件R.java
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.eirc.linear; public final class R { public static final class attr { } public static final class color { public static final int calamus=0x7f070000; } public static final class dimen { /** Default screen margins, per the Android Design guidelines. Customize dimensions originally defined in res/values/dimens.xml (such as screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here. */ public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int LinearLayout=0x7f090000; public static final int action_settings=0x7f090005; public static final int button1=0x7f090001; public static final int button2=0x7f090002; public static final int button3=0x7f090003; public static final int button4=0x7f090004; } public static final class layout { public static final int activity_my=0x7f030000; } public static final class menu { public static final int my=0x7f080000; } public static final class string { public static final int action_settings=0x7f050001; public static final int app_name=0x7f050000; public static final int hao=0x7f050004; public static final int ren=0x7f050005; public static final int shi=0x7f050003; public static final int wo=0x7f050002; } public static final class style { /** Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. Base application theme for API 11+. This theme completely replaces AppBaseTheme from res/values/styles.xml on API 11+ devices. API 11 theme customizations can go here. Base application theme for API 14+. This theme completely replaces AppBaseTheme from BOTH res/values/styles.xml and res/values-v11/styles.xml on API 14+ devices. API 14 theme customizations can go here. */ public static final int AppBaseTheme=0x7f060000; /** Application theme. All customizations that are NOT specific to a particular API-level can go here. */ public static final int AppTheme=0x7f060001; } }
Activity:
package com.eirc.linear; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my, menu); return true; } }
最终效果:
http://www.zhangjianghome.net/
最近公司需要, 图片从应用里面分离出来, 如果将图片服务和应用服务放在同一个服务器的话,应用服务器很容易会因为图片的高I/O负载而崩溃,因此对于有些大型网站项目,我们有必要将图片服务器和应用服务器分离。下图是我画的一个简单的用户->应用服务器->nfs图片服务器的一个流转草图:
从图片上分析:
192.168.1.252 作为服务器 192.168.1.251
作为客户端,
一: 首先在应用服务器和图片服务器都安装好NFS, 一般系统都默认安装了nfs, 我们只需要对nfs进行配置,重启就可以用了
nfs服务器:
vi /etc/exports
在里面加入需要共享的文件目录和对应的客户端地址及权限
添加: /home/share 192.168.1.251(rw,no_root_squash,sync)
执行: exportfs -r ,重新刷新配置
二,在应用服务器挂载第一台nfs共享的文件目录
mount -t nfs 192.168.1.252:/nfs/photos/ /mnt/flimg0/
这样如果没有出问题的话, 挂载就成功了
三: 那怎么能够访问到图片服务器的图片呢
我们在应用服务器安装nginx, 配置静态目录, 目录对应/mnt/flimg0 , 这样就可以访问了.
太多的细节我就不写了...