您的位置:首页 > 运维架构 > Nginx

Nginx主配置参数详解,Nginx配置网站

2017-01-22 17:42 417 查看
阅读目录
1.Niginx主配置文件参数详解
2.Nginx.conf配置文件详细说明(附备注)
3.Nginx代理网站

回到顶部


1.Niginx主配置文件参数详解

  a.上面博客说了在Linux中安装nginx。博文地址为:http://www.cnblogs.com/hanyinglong/p/5102141.html

  b.当Nginx安装完毕后,会有相应的安装目录,安装目录里的nginx.confg为nginx的主配置文件,nginx主配置文件分为4部分,main(全局配置)、server(主机配置)、upstream(负载均衡服务器设置)以及location(URL匹配特定位置的设置),这四者的关系是:server继承main,location继承server,upstream既不会继承其它设置也不会被继承。

  c.Nginx是一个代理服务器,一般情况下,网站是不能部署在Nginx下的,比如用Java开发的JavaWeb程序,我们部署在tomcat下,然后使用Nginx代理将网址指向tomcat即可。

回到顶部


2.Nginx.conf配置文件详细说明(附备注)

1 #  kencery 注释说明Nginx文件
2 #  时间:2016-1-19
3 #  学习内容,只是来自互联网,有版权问题请联系我删除。
4
5 ########   Nginx的main(全局配置)文件
6 #指定nginx运行的用户及用户组,默认为nobody
7 #user  nobody;
8
9 #开启的线程数,一般跟逻辑CPU核数一致
10 worker_processes  1;
11
12 #定位全局错误日志文件,级别以notice显示,还有debug,info,warn,error,crit模式,debug输出最多,crir输出最少,根据实际环境而定
13 #error_log  logs/error.log;
14 #error_log  logs/error.log  notice;
15 #error_log  logs/error.log  info;
16
17 #指定进程id的存储文件位置
18 #pid        logs/nginx.pid;
19
20 #指定一个nginx进程打开的最多文件描述符数目,受系统进程的最大打开文件数量限制
21 #worker_rlimit_nofile 65535
22
23 events {
24     #设置工作模式为epoll,除此之外还有select,poll,kqueue,rtsig和/dev/poll模式
25     #use epoll;
26
27     #定义每个进程的最大连接数,受系统进程的最大打开文件数量限制。
28     worker_connections  1024;
29 }
30
31 #######Nginx的Http服务器配置,Gzip配置
32 http {
33     #主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度,DNS主配置文件中的zonerfc1912,acl基本上都是用include语句。
34     include       mime.types;
35
36     #核心模块指令,智力默认设置为二进制流,也就是当文件类型未定义时使用这种方式
37     default_type  application/octet-stream;
38
39     #下面代码为日志格式的设定,main为日志格式的名称,可自行设置,后面引用
40     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
41     #                  '$status $body_bytes_sent "$http_referer" '
42     #                  '"$http_user_agent" "$http_x_forwarded_for"';
43
44     #引用日志main
45     #access_log  logs/access.log  main;
46
47     #设置允许客户端请求的最大的单个文件字节数
48     #client_max_body_size 20M;
49     #指定来自客户端请求头的headebuffer大小
50     #client_header_buffer_size  32k;
51     #指定连接请求试图写入缓存文件的目录路径
52     #client_body_temp_path /dev/shm/client_body_temp;
53     #指定客户端请求中较大的消息头的缓存最大数量和大小,目前设置为4个32KB
54     #large client_header_buffers 4 32k;
55
56     #开启高效文件传输模式
57     sendfile        on;
58     #开启防止网络阻塞
59     #tcp_nopush     on;
60     #开启防止网络阻塞
61     #tcp_nodelay    on;
62
63     #设置客户端连接保存活动的超时时间
64     #keepalive_timeout  0;
65     keepalive_timeout  65;
66
67     #设置客户端请求读取超时时间
68     #client_header_timeout 10;
69     #设置客户端请求主体读取超时时间
70     #client_body_timeout 10;
71     #用于设置相应客户端的超时时间
72     #send_timeout
73
74     ####HttpGZip模块配置
75     #httpGzip modules
76     #开启gzip压缩
77     #gzip  on;
78     #设置允许压缩的页面最小字节数
79     #gzip_min_length 1k;
80     #申请4个单位为16K的内存作为压缩结果流缓存
81     #gzip_buffers 4 16k;
82     #设置识别http协议的版本,默认为1.1
83     #gzip_http_version 1.1;
84     #指定gzip压缩比,1-9数字越小,压缩比越小,速度越快
85     #gzip_comp_level 2;
86     #指定压缩的类型
87     #gzip_types text/plain application/x-javascript text/css application/xml;
88     #让前端的缓存服务器进过gzip压缩的页面
89     #gzip_vary on;
90
91     #########Nginx的server虚拟主机配置
92     server {
93         #监听端口为 80
94         listen       80;
95
96         #设置主机域名
97         server_name  localhost;
98
99         #设置访问的语言编码
100         #charset koi8-r;
101
102         #设置虚拟主机访问日志的存放路径及日志的格式为main
103         #access_log  logs/host.access.log  main;
104
105         #设置虚拟主机的基本信息
106         location / {
107             #设置虚拟主机的网站根目录
108             root   html;
109
110             #设置虚拟主机默认访问的网页
111             index  index.html index.htm;
112         }
113
114         #error_page  404              /404.html;
115
116         # redirect server error pages to the static page /50x.html
117         #
118         error_page   500 502 503 504  /50x.html;
119         location = /50x.html {
120             root   html;
121         }
122
123         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
124         #
125         #location ~ \.php$ {
126         #    proxy_pass   http://127.0.0.1; 127         #}
128
129         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
130         #
131         #location ~ \.php$ {
132         #    root           html;
133         #    fastcgi_pass   127.0.0.1:9000;
134         #    fastcgi_index  index.php;
135         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
136         #    include        fastcgi_params;
137         #}
138
139         # deny access to .htaccess files, if Apache's document root
140         # concurs with nginx's one
141         #
142         #location ~ /\.ht {
143         #    deny  all;
144         #}
145     }
146
147
148     # another virtual host using mix of IP-, name-, and port-based configuration
149     #
150     #server {
151     #    listen       8000;
152     #    listen       somename:8080;
153     #    server_name  somename  alias  another.alias;
154
155     #    location / {
156     #        root   html;
157     #        index  index.html index.htm;
158     #    }
159     #}
160
161
162     # HTTPS server
163     #
164     #server {
165     #    listen       443 ssl;
166     #    server_name  localhost;
167
168     #    ssl_certificate      cert.pem;
169     #    ssl_certificate_key  cert.key;
170
171     #    ssl_session_cache    shared:SSL:1m;
172     #    ssl_session_timeout  5m;
173
174     #    ssl_ciphers  HIGH:!aNULL:!MD5;
175     #    ssl_prefer_server_ciphers  on;
176
177     #    location / {
178     #        root   html;
179     #        index  index.html index.htm;
180     #    }
181     #}
182
183 }


回到顶部


3.Nginx代理网站

  a.我在tomcat下部署了一个javaweb项目,tomcat安装的服务器IP为:192.168.37.136,部署的项目在tomcat下的访问地址为:http://192.168.37.136:8080/lywh/

  b.我在IP为192.168.37.133的服务器下面安装成功了Nginx。

  c.那怎么样将tomcat下部署的网站使用Nginx代理呢?,修改Nginx的配置文件,修改命令:vim /usr/local/nginx/conf/nginx.conf

1 #user  nobody;
2 worker_processes  1;
3 #error_log  logs/error.log;
4 #error_log  logs/error.log  notice;
5 #error_log  logs/error.log  info;
7 #pid        logs/nginx.pid;
10 events {
11     worker_connections  1024;
12 }
15 http {
16     include       mime.types;
17     default_type  application/octet-stream;
18
19     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
20     #                  '$status $body_bytes_sent "$http_referer" '
21     #                  '"$http_user_agent" "$http_x_forwarded_for"';
22
23     #access_log  logs/access.log  main;
24
25     sendfile        on;
26     #tcp_nopush     on;
27
28     #keepalive_timeout  0;
29     keepalive_timeout  65;
30
31     #gzip  on;
32
33     #配置tomcat的IP地址和访问端口
34     upstream gw {
35         server 192.168.37.136:8080 weight=1;
36     }
37     server {
38         listen       80;
39         server_name  localhost;
40
41         #charset koi8-r;
42
43         #access_log  logs/host.access.log  main;
44
45         location / {
46             root   html;
47             index  index.html index.htm;
48         }
49     #Nginx代理配置
50     location /lywh {
51         proxy_pass http://gw/lywh; 52     }
53     location /sapi {
54         proxy_pass http://gw/shopappapi; 55     }
56     location /cas{
57         proxy_pass http://gw/cas-server-webapp-4.0.0/login; 58     }
59     location /doc{
60         proxy_pass http://gw/docs; 61     }
62
63     #error_page  404              /404.html;
64
65         # redirect server error pages to the static page /50x.html
66         #
67         error_page   500 502 503 504  /50x.html;
68         location = /50x.html {
69             root   html;
70         }
71
72         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
73         #
74         #location ~ \.php$ {
75         #    proxy_pass   http://127.0.0.1; 76         #}
77
78         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
79         #
80         #location ~ \.php$ {
81         #    root           html;
82         #    fastcgi_pass   127.0.0.1:9000;
83         #    fastcgi_index  index.php;
84         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
85         #    include        fastcgi_params;
86         #}
87
88         # deny access to .htaccess files, if Apache's document root
89         # concurs with nginx's one
90         #
91         #location ~ /\.ht {
92         #    deny  all;
93         #}
94     }
95
96
97     # another virtual host using mix of IP-, name-, and port-based configuration
98     #
99     #server {
100     #    listen       8000;
101     #    listen       somename:8080;
102     #    server_name  somename  alias  another.alias;
103
104     #    location / {
105     #        root   html;
106     #        index  index.html index.htm;
107     #    }
108     #}
109
110
111     # HTTPS server
112     #
113     #server {
114     #    listen       443 ssl;
115     #    server_name  localhost;
116
117     #    ssl_certificate      cert.pem;
118     #    ssl_certificate_key  cert.key;
119
120     #    ssl_session_cache    shared:SSL:1m;
121     #    ssl_session_timeout  5m;
122
123     #    ssl_ciphers  HIGH:!aNULL:!MD5;
124     #    ssl_prefer_server_ciphers  on;
125
126     #    location / {
127     #        root   html;
128     #        index  index.html index.htm;
129     #    }
130     #}
131
132 }


  d.当配置完Nginx.conf之后,关闭文件,执行命令检查配置的文件是否有问题,如果如图所示则说明没有问题,否则需要检查配置是否出现问题

    


  e.检查如果返回ok,则说明修改文件没有出现任何错误,这时候重启Nginx,命令为: /usr/local/nginx/sbin/nginx -s reload

  f.最后访问代理后的网站,http://192.168.37.133/lywh,如图所示:则说明已经代理访问:

    


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: