配置文件
nginx.conf
主配置文件,存放在/etc/nginx(此目录为nginx使用apt或者yum这种)
#运行用户,默认即是nginx,可以不进行设置user nginx;#Nginx进程,一般设置为和CPU核数一样worker_processes 1;#错误日志存放目录error_log /var/log/nginx/error.log warn;#进程pid存放位置pid /var/run/nginx.pid;events {worker_connections 1024; # 单个后台进程的最大并发数}http {include /etc/nginx/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 /var/log/nginx/access.log main; #nginx访问日志存放位置sendfile on; #开启高效传输模式#tcp_nopush on; #减少网络报文段的数量keepalive_timeout 65; #保持连接的时间,也叫超时时间#gzip on; #开启gzip压缩include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件}
default.conf
默认子配置文件,在/etc/nginx/conf.d中,是nginx.conf最后一行加载的
server {listen 80; #配置监听端口server_name localhost; //配置域名#charset koi8-r;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html; #服务默认启动目录index index.html index.htm; #默认访问文件}#error_page 404 /404.html; # 配置404页面# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启location = /50x.html {root /usr/share/nginx/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;#}}
常见用途
反向代理
以下操作将代理http://127.0.0.1:8090到http://127.0.0.1:80/facts_backend-2.6/
location /facts_backend-2.6/ {index index.html index.htm index.jsp index.action;proxy_set_header X-Real-IP $remote_addr;proxy_set_header Host $host;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Server $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://127.0.0.1:8090/;}
文件服务器
tips:/地址可以使用root路径,其他映射的地址必须添加alias前缀
