【linux】linux:nginx 简单 web 配置设置使用示例

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39720249/article/details/84663881

目录

一、一个简单的Nginx转发例子

二、例子2(暴露接口,但隐藏web项目的根目录)

三、例子3 文件映射



一、一个简单的Nginx转发例子

(1)需求

①短信服务器列表三台,提供服务的地址如下:
http://192.168.88.21:8091/smsserver/services/sendSms?wsdl
http://192.168.88.22:8091/smsserver/services/sendSms?wsdl
http://192.168.88.23:8091/smsserver/services/sendSms?wsdl

②彩信服务器列表三台,提供服务的地址如下:
http://192.168.88.21:8092/msserver/services/sendMms?wsdl
http://192.168.88.22:8092/mmsserver/services/sendMms?wsdl
http://192.168.88.23:8092/mmsserver/services/sendMms?wsdl 

③Nginx安装在另外一台单独机器上(公网IP为public_ip),对外提供服务地址如下:
http://public_ip:8090/smsserver/services/sendSms?wsdl 短信发送服务
http://public_ip:8090/mmsserver/services/sendMms?wsdl 彩信发送服务

(2)对于以上需求,配置/opt/nginx-1.8.0/conf/nginx.conf如下:

#user nobody; 
worker_processes 1; 

#error_log logs/error.log; 
#error_log logs/error.log notice; 
#error_log logs/error.log info; 

#pid logs/nginx.pid; 

events { 
worker_connections 1024; 
} 

http { 
include mime.types; 
default_type application/octet-stream; 

#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
# '$status $body_bytes_sent "$http_referer" ' 
# '"$http_user_agent" "$http_x_forwarded_for"'; 

#access_log logs/access.log main; 

sendfile on; 
#tcp_nopush on; 

#keepalive_timeout 0; 
keepalive_timeout 65; 
#设定请求缓冲, start 
client_header_buffer_size 1k; 
large_client_header_buffers 4 4k; 
#设定请求缓冲, end 

#设定提供服务的服务器,start 
#短信发送服务器 
upstream smsserver{ 
#weigth 表示权重,权值越大,分配几率越大 
#max_fails 当有max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查 
#fail_timeout 在以后的fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器 
server 192.168.88.21:8091 weight=5 max_fails=5 fail_timeout=600s; 
server 192.168.88.22:8091 weight=5 max_fails=5 fail_timeout=600s; 
server 192.168.88.23:8091 weight=5 max_fails=5 fail_timeout=600s; 
} 
#彩信发送服务器 
upstream mmsserver{ 
#weigth 表示权重,权值越大,分配几率越大 
#max_fails 当有max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查 
#fail_timeout 在以后的fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器 
server 192.168.88.21:8092 weight=5 max_fails=5 fail_timeout=600s; 
server 192.168.88.22:8092 weight=5 max_fails=5 fail_timeout=600s; 
server 192.168.88.23:8092 weight=5 max_fails=5 fail_timeout=600s; 
} 
#设定提供服务的服务器,end 

#gzip on; 

server { 
listen 8090; 
server_name localhost; 
#设定请求转发规则, start 
#规则采用最长匹配,/smsserver/*优先匹配/smsserver,/mmsserver/*优先匹配/mmsserver,/aaaaaaaa/*因为没有任何匹配,最后匹配到/ 
#规则一 
location / { 
proxy_pass http://localhost:80; 
} 
#规则二 
location /smsserver { 
proxy_pass http://smsserver; 
} 
#规则三 
location /mmsserver { 
proxy_pass http://mmsserver; 
} 
#设定请求转发规则, end 
error_page 500 502 503 504 /50x.html; 
location = /50x.html { 
root html; 
} 
} 

server { 
listen 80; 
server_name localhost; 
#设定请求转发规则, start 
location / { 
#定义服务器的默认网站根目录位置 
root /home/work/statichtml/index.html; 
#定义首页索引文件的名称 
#index index.php index.html index.htm; 
#请求转向orderServer定义的服务器列表 
# proxy_pass http://server; 

#以下是一些反向代理的配置可删除. 
# proxy_redirect off; 
# proxy_set_header Host $host; 
# proxy_set_header X-Real-IP $remote_addr; 
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
#允许客户端请求的最大单文件字节数 
# client_max_body_size 10m; 
#缓冲区代理缓冲用户端请求的最大字节数, 
# client_body_buffer_size 128k; 
#nginx跟后端服务器连接超时时间(代理连接超时) 
# proxy_connect_timeout 90; 
#后端服务器数据回传时间(代理发送超时) 
# proxy_send_timeout 90; 
#连接成功后,后端服务器响应时间(代理接收超时) 
# proxy_read_timeout 90; 
#设置代理服务器(nginx)保存用户头信息的缓冲区大小 
# proxy_buffer_size 4k; 
#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 
# proxy_buffers 4 32k; 
#高负荷下缓冲大小(proxy_buffers*2) 
# proxy_busy_buffers_size 64k; 
#设定缓存文件夹大小,大于这个值,将从upstream服务器传 
# proxy_temp_file_write_size 64k; 
} 
#设定请求转发规则, end 
error_page 500 502 503 504 /50x.html; 
location = /50x.html { 
root html; 
} 
} 
} 


二、例子2(暴露接口,但隐藏web项目的根目录)

上面例子有个问题是,直接将我整个web根目录smsserver和mmsserver暴露到公网了,其实我的目的只是想暴露两个接口地址,所以有待优化,举个例子如下:
(1)需求

①接口服务器列表两台台,提供服务的地址如下:
Ⅰ.接口一:
http://192.168.88.21:8082/myweb/interface/getData
http://192.168.88.22:8082/myweb/interface/getData
Ⅱ.接口二:
http://192.168.88.21:8082/myweb/interface/sendData
http://192.168.88.22:8082/myweb/interface/sendData

②Nginx安装在另外一台单独机器上(公网IP为public_ip),对外提供服务地址如下: 
http://public_ip:8081/myweb/interface/getData 接口一 
http://public_ip:8081/myweb/interface/sendData 接口二

(2)对于以上需求,配置/opt/nginx-1.8.0/conf/nginx.conf如下:

#user nobody; 
worker_processes 1; 
#error_log logs/error.log; 
#error_log logs/error.log notice; 
#error_log logs/error.log info; 
#pid logs/nginx.pid; 
events { 
worker_connections 1024; 
} 
http { 
include mime.types; 
default_type application/octet-stream; 

#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
# '$status $body_bytes_sent "$http_referer" ' 
# '"$http_user_agent" "$http_x_forwarded_for"'; 

#access_log logs/access.log main; 

sendfile on; 
#tcp_nopush on; 

#keepalive_timeout 0; 
keepalive_timeout 65; 
#设定请求缓冲, start 
client_header_buffer_size 1k; 
large_client_header_buffers 4 4k; 
#设定请求缓冲, end 

#设定提供服务的服务器,start 
#我的接口服务器,两台 
upstream myweb{ 
#weigth 表示权重,权值越大,分配几率越大 
#max_fails 当有max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查 
#fail_timeout 在以后的fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器 
server 192.168.88.23:8082 weight=5 max_fails=5 fail_timeout=600s; 
server 192.168.88.24:8082 weight=5 max_fails=5 fail_timeout=600s; 
} 
#设定提供服务的服务器,end 

#gzip on; 

server { 
listen 80; 
server_name localhost; 
#设定请求转发规则, start 
#规则采用最长匹配,即长度最长优先匹配,最后不匹配的走/进行匹配 
#规则一 
location / { 
root html; 
index index.html index.htm; 
} 
#设定请求转发规则, end 
error_page 500 502 503 504 /50x.html; 
location = /50x.html { 
root html; 
} 
} 

server { 
listen 8081; 
server_name localhost; 
#设定请求转发规则, start 
#规则采用最长匹配,即长度最长优先匹配,最后不匹配的走/进行匹配 
#规则一 
location / { 
root html; 
index index.html index.htm; 
} 
#规则二,查询接口一 
location /myweb/interface/getData { 
proxy_pass http://myweb; 
#转发请求的原IP地址,程序中通过request.getHeader("Proxy-Client-IP")获得ip 
proxy_set_header Host $host; 
#如果是有涉及redirect的服务,一定要加上端口8081,否则默认tomcat在redirect时候默认找80端口 
#proxy_set_header Host $host:8081; 
proxy_set_header Proxy-Client-IP $remote_addr; 
} 
#规则三,查询接口二 
location /myweb/interface/sendData { 
proxy_pass http://myweb; 
#转发请求的原IP地址,程序中通过request.getHeader("Proxy-Client-IP")获得ip 
proxy_set_header Host $host; 
#如果是有涉及redirect的服务,一定要加上端口8081,否则默认tomcat在redirect时候默认找80端口 
#proxy_set_header Host $host:8081; 
proxy_set_header Proxy-Client-IP $remote_addr; 
} 
#设定请求转发规则, end 
error_page 500 502 503 504 /50x.html; 
location = /50x.html { 
root html; 
} 
} 
} 

三、例子3 文件映射

/conf/nginx.conf中第一行的运行用户改为
user root;
location /uploadfile {
root /opt/staticdata/;
access_log /opt/nginx/logs/upload_access.log;
proxy_store_access user:rw group:rw all:rw;
}

当访问http://ip:port/uploadfile/aa/bb/cc.jpg
相当于访问 /opt/staticdata/uploadfile/aa/bb/cc.jpg

 

展开阅读全文

Linux配置nginx

12-20

线上nginx常用配置是哪些?rnrn[code=php]rn 1 rn 2 #user nobody;rn 3 worker_processes 1;rn 4 rn 5 #error_log logs/error.log;rn 6 #error_log logs/error.log notice;rn 7 #error_log logs/error.log info;rn 8 rn 9 #pid logs/nginx.pid;rn 10 rn 11 worker_rlimit_core 50M;rn 12 working_directory /worker_core_tmp/;rn 13 rn 14 events rn 15 worker_connections 1024;rn 16 rn 17 rn 18 rn 19 http rn 20 include mime.types;rn 21 default_type application/octet-stream;rn 22 rn 23 #log_format main '$remote_addr - $remote_user [$time_local] "$request" 'rn 24 # '$status $body_bytes_sent "$http_referer" 'rn 25 # '"$http_user_agent" "$http_x_forwarded_for"';rn 26 rn 27 #access_log logs/access.log main;rn 28 rn 29 sendfile on;rn 30 #tcp_nopush on;rn 31 rn 32 #keepalive_timeout 0;rn 33 keepalive_timeout 65;rn 34 rn 35 #gzip on;rn 36 rn 37 server rn 38 listen 80;rn 39 server_name localhost;rn 40 rn 41 #charset koi8-r;rn42 rn 43 #access_log logs/host.access.log main;rn 44 rn 45 location / rn 46 root html;rn 47 index index.html index.htm;rn 48 rn 49 rn 50 #error_page 404 /404.html;rn 51 rn 52 # redirect server error pages to the static page /50x.htmlrn 53 #rn 54 error_page 500 502 503 504 /50x.html;rn 55 location = /50x.html rn 56 root html;rn 57 rn 58 rn 59 # proxy the PHP scripts to Apache listening on 127.0.0.1:80rn 60 #rn 61 #location ~ \.php$ rn 62 # proxy_pass http://127.0.0.1;rn 63 #rn 64 rn 65 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000rn 66 #rn 67 #location ~ \.php$ rn 68 # root html;rn 69 # fastcgi_pass 127.0.0.1:9000;rn 70 # fastcgi_index index.php;rn 71 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;rn 72 # include fastcgi_params;rn 73 #rn 74 rn 75 # deny access to .htaccess files, if Apache's document rootrn 76 # concurs with nginx's onern 77 #rn 78 #location ~ /\.ht rn 79 # deny all;rn 80 #rn rn 82 rn 83 rn 84 # another virtual host using mix of IP-, name-, and port-based configurationrn 85 #rn 86 #server rn 87 # listen 8000;rn 88 # listen somename:8080;rn 89 # server_name somename alias another.alias;rn 90 rn 91 # location / rn 92 # root html;rn 93 # index index.html index.htm;rn 94 # rn 95 #rn 96 rn 97 rn 98 # HTTPS serverrn 99 #rn100 #server rn101 # listen 443 ssl;rn102 # server_name localhost;rn103 rn104 # ssl_certificate cert.pem;rn105 # ssl_certificate_key cert.key;rn106 rn107 # ssl_session_cache shared:SSL:1m;rn108 # ssl_session_timeout 5m;rn109 rn110 # ssl_ciphers HIGH:!aNULL:!MD5;rn111 # ssl_prefer_server_ciphers on;rn112 rn113 # location / rn114 # root html;rn115 # index index.html index.htm;rn116 # rn117 #rn118rn119 rnrn[/code] 论坛

没有更多推荐了,返回首页