由于涉及到h5与后端交互,跨域问题,所以公司的开放测试服务器让我们自己搞Nginx。顺便提升一下Nginx的实践。
Nginx的安装,没什么难度了,百度一堆,如果源码安装就一步步来吧。(最简单的方式:yum install Nginx (centos),apt-get install Nginx(ubuntu)) Nginx.conf,作为最外层的配置文件,主要设置一些基础的配置就好了,如内存配置,日志格式配置,线程配置等,最后使用一个include conf.d/* 将其他配置文件包含进来即可。
【Nginx.conf 基础配置】
error_log /data/var/log/Nginx/<span >error.log deBUG;error_log logs/<span >error.log notice;error_log logs/error.log <span >info<span >;pID logs/<span >Nginx.pID;events {
worker_connections <span >1024<span >;
}
http {
include mime.types;
default_type application/octet-<span >stream;
autoindex off;
server_tokens off;
server_<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>s_hash_bucket_size </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">128</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">;cl<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>nt_<a href="https://m.jb51.cc/tag/header/" target="_blank" >header</a>_buffer_size 32k;large_cl<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>nt_<a href="https://m.jb51.cc/tag/header/" target="_blank" >header</a>_buffers </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">4</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> 32k;cl<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>nt_max_body_size 20m;cl<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>nt_body_buffer_size 256k;send<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a> on;tcp_<a href="https://www.jb51.cc/tag/nop/" target="_blank" >nop</a>ush on;keepa<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ve_timeout </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">60</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">;tcp_nodelay on;fastcgi_connect_timeout </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">300</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">;fastcgi_send_timeout </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">300</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">;fastcgi_read_timeout </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">300</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">;fastcgi_buffer_size 128k;fastcgi_buffers </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">32</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> 256k;fastcgi_busy_buffers_size 256k;fastcgi_temp_<a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a>_write_size 256k;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">gzip</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> on;gzip_<a href="https://www.jb51.cc/tag/dis/" target="_blank" >dis</a>able </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">ms<a href="https://m.jb51.cc/tag/ie6/" target="_blank" >ie6</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">;gzip_<a href="https://www.jb51.cc/tag/vary/" target="_blank" >vary</a> on;gzip_comp_level </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">2</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">;gzip_min_length 1k;gzip_buffers </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">4</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> 16k;gzip_<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>_version </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1.1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">;gzip_types text</span>/plain app<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>cation/x-<a href="https://m.jb51.cc/tag/JavaScript/" target="_blank" >JavaScript</a> text/<a href="https://m.jb51.cc/tag/CSS/" target="_blank" >CSS</a> app<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>cation/xml app<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>cation/<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"><a href="https://m.jb51.cc/tag/JavaScript/" target="_blank" >JavaScript</a>;log_format main </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">$request_time $upstream_response_time $remote_addr - $upstream_addr [$time_local] </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"$host" "$request" $status $bytes_sent </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"$<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>_referer" "$<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>_user_agent" "$gzip_ratio" "$<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>_x_forwarded_for" - "$server_addr" </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">;access_log </span>/data/var/log/<a href="https://www.jb51.cc/tag/Nginx/" target="_blank" >Nginx</a>/<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">access.log main;include conf.d</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">.conf;
}
【conf.d/*,具体的域名配置,http://】
.:.:.:.:server {Listen <span >80<span >;
server_name 3ctest.x123.com;
location /<span > {
proxy_pass http:<span >//<span >3ctest_x123_com;
<span > proxysetheader Host $host;
proxy_redirect off;
proxysetheader X-Real-<span >IP $remote_addr;
proxysetheader X-Forwarded-<span >For $proxy_add_x_forwarded_for;
proxy_connect_timeout <span >60<span >;
proxy_read_timeout <span >600<span >;
proxy_send_timeout <span >600<span >;
}
}
server {
Listen
server_name mytest.x123.com;
location /<span > {
proxy_pass http:<span >//<span >mytest_x123_com;
<span > proxysetheader Host $host;
proxy_redirect off;
proxysetheader X-Real-<span >IP $remote_addr;
proxysetheader X-Forwarded-<span >For $proxy_add_x_forwarded_for;
proxy_connect_timeout <span >60<span >;
proxy_read_timeout <span >600<span >;
proxy_send_timeout <span >600<span >;
}
}
server {
Listen <span >80<span >;
server_name 3cAPI.x123.com;
location /<span > {
proxy_pass http:<span >//<span >3cAPI_x123_com;
<span > proxysetheader Host $host;
proxy_redirect off;
proxysetheader X-Real-<span >IP $remote_addr;
proxysetheader X-Forwarded-<span >For $proxy_add_x_forwarded_for;
proxy_connect_timeout <span >60<span >;
proxy_read_timeout <span >600<span >;
proxy_send_timeout <span >600<span >;
}
}
server {
Listen <span >80<span >;
server_name yhAPI.x123.com;
location /<span > {
proxy_pass http:<span >//<span >yhAPI_x123_com;
<span > proxysetheader Host $host;
proxy_redirect off;
proxysetheader X-Real-<span >IP $remote_addr;
proxysetheader X-Forwarded-<span >For $proxy_add_x_forwarded_for;
proxy_connect_timeout <span >60<span >;
proxy_read_timeout <span >600<span >;
proxy_send_timeout <span >600<span >;
}
}
server {
Listen <span >80<span >;
server_name <span >192.168.<span >1.22<span >;
location /<span > {
proxy_pass http:<span >//<span >192.168.1.22;
<span > proxysetheader Host $host;
proxy_redirect off;
proxysetheader X-Real-<span >IP $remote_addr;
proxysetheader X-Forwarded-<span >For $proxy_add_x_forwarded_for;
proxy_connect_timeout <span >60<span >;
proxy_read_timeout <span >600<span >;
proxy_send_timeout <span >600<span >;
}
}
upstream <span >192.168.<span >1.22<span > {
server <span >192.168.<span >1.22:<span >88<span >;
keepalive <span >8<span >;
}
【https:// 配置】
/etc/Nginx/conf.d/ssl//etc/Nginx/conf.d/ssl/ssl_prefer_server_ciphers on;ssl_protoc<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>s TLSv1 TLSv1.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1</span> TLSv1.<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">2</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">;ssl_ciphers ECDHE</span>-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aN<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>L:!eN<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>L:!EXPORT:!CAMEL<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>A:!DES:!MD5:!PSK:!<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">RC4;location </span>/<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> { #proxy_set_<a href="https://m.jb51.cc/tag/header/" target="_blank" >header</a> Host $<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>_host;#proxy_set_<a href="https://m.jb51.cc/tag/header/" target="_blank" >header</a> Host $<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>_host;#proxy_set_<a href="https://m.jb51.cc/tag/header/" target="_blank" >header</a> X</span>-Forwarded-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">For $remote_addr;# on<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne #proxy_redirect <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">192.168.1.22/ <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">$<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>_host/;</span> #proxy_pass <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>:<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">shmc.mysite1.com;</span>
<span >
proxy_pass http:<span >//<span >192.168.1.22/;<span > #index index.HTML;
root /data/www/<span >;<span >if ( $cookie_cookie ~ <span >"<span >(.)$<span >"<span >) {set $all_cookie $<span >1<span >;}proxysetheader cookie <span >"<span >$http_cookie; node_ID=018<span >"<span >; proxy_set_<a href="https://m.jb51.cc/tag/header/" target="_blank" >header</a> X</span>-Real-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">IP $remote_addr; proxy_set_<a href="https://m.jb51.cc/tag/header/" target="_blank" >header</a> X</span>-Forwarded-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">For $proxy_add_x_forwarded_for; proxy_set_<a href="https://m.jb51.cc/tag/header/" target="_blank" >header</a> Host $<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>_host;add_<a href="https://m.jb51.cc/tag/header/" target="_blank" >header</a> Access</span>-Contr<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>-Allow-Origin *<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">; proxy_pass <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">192.168.1.22;</span>
<span > proxy_redirect off;
}
}
server {
Listen <span >80<span >;
server_name wx.mysite1.com;
location /<span > {
proxysetheader Host $host;
#proxy_redirect <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">192.168.1.22/ <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">$<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>_host/;</span> #proxy_pass <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>:<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">shmc.mysite1.com;</span> proxy_pass <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>:<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">192.168.1.22/;</span>
<span > #index index.HTML;
root /data/www/<span >;}
}
如上参考,应该可以解决大部分配置情况。
需要注意的是,Nginx作为反向代理服务器,很多变量需要作特别转发,如ip地址,有问题可以先查看日志,如果语法错误,则不会被保存,这个比较好!
Nginx作为负载均衡的重要软件,略加掌握总归是好的吧。
其实也不难,但是没有实战的谈话总是显得苍白无力!要做到好也不简单,一个简单的优化,可以让你的用户体验更好,服务器利用率更高,有得搞!
勉励吧诸君!
总结以上是内存溢出为你收集整理的nginx 配置管理 - 简单也复杂全部内容,希望文章能够帮你解决nginx 配置管理 - 简单也复杂所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)