学会配置nginx

时间:2022-12-20 15:10:10

一、作为一名开发人员,大家可能经常会用到服务器,但是一般线上的服务器可能都是公司公用的,而且线上的服务器一般也不是能随随便便给个人用的,所以部署本地服务器看来是一遍必不可少的事情和能力呀,所以,nginx腾空而出,它可以在任何一款vim终端配置部署,只要你能配置的好,它就是一款很不错的本地服务器,对你的开发有不小的帮助;

二、好了,废话不多说,接下来我给大家贴一个自己以往配置过的nginx配置:

##运行用户
#user nobody;
#启动进程,通常设置程和cpu的数量相等
worker_processes 1;

#全局错误日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

##工作模式及连接数上限
events {
####epoll是多路复用IO(I/O Multiplexing)中的一种方式
#use epoll

###单个后台worker process 进程的最大并发链接数
worker_connections 1024;
}

http {
###设定mime类型,类型由mime.type文件定义
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指令指定nginx是否调用sendfile函数(zero copy方式)来输出文件,
##对于普通应用,必须设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
#以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;

#开启gzip压缩
#gzip on;
#设定虚拟主机配置
server {
#侦听80端口
listen 8080;
#定义使用 localhost访问
server_name localhost;

#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;

#location / {
#定义服务器的默认网站根目录位置
# root html;
# index index.html index.htm;
#}

location /rbac/ {
proxy_pass http://b1.rbac.wormpex.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
}

location /home/ {
proxy_pass http://0.0.0.0:8004;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
}

location /ticket/ { 
proxy_pass http://0.0.0.0:8002/;
#####设置被代理服务器的端口或套接字,以及URL
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
####以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上
client_max_body_size 10m;
}

#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;
}
# location / {
# root /usr/share/nginx/html;
# index index.html;
# }

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# #include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}

# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
#include vhost/*.conf;
}

好了,这是自己实习期间部署的第一个nginx配置,大家可以根据自己的项目,作相应的改动;