nginx 初尝试
Posted 海上小波
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx 初尝试相关的知识,希望对你有一定的参考价值。
nginx功能
nginx至少具有一个部署静态资源的功能,默认部署端口为80, 通过配置nginx还可同时监听其他端口。- nginx可以用来做静态资源服务器,部署html、图片等静态资源。(通过一个php库还可以部署php,具体怎么搞还没有弄,java web项目是部署不了了,只能在nginx转发请求到java web服务器);
- 用来做请求转发(反向代理),如果转发给内网服务器就叫内网映射,如果一个端口配置可转发到多个域就是负载均衡。
Nginx配置
nginx的配置文件是nginx.conf, 我的nginx是用arch的yaourt命令安装的,配置文件被安装到了/etc/nginx/nginx.conf, nginx具有一个默认监听服务(80端口),配置文件在http节点下面。如果还想配置其他端口的监听可以平行http端口再设置Server节点,如下是我的一个nginx.conf配置文件: 注意server_name只是一个名称而已,不具有其他意义,刚开始我还以为nginx反向代理会涉及三个服务器一个是无法访问内网的公网ip服务器(如阿里云),第二个是内网nginx服务器,第三个是内网其他服务器 (内网服务器都没有公网ip), server_name就是用来设置公网ip服务器的, 显然这种认为是错的, nginx反向代理只涉及两个服务器,第一个是具备公网ip的内网服务器上面跑nginx服务,另一个就是内网其他服务器(不具备公网ip)。#user html;
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;
#gzip on;
server
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /
root /usr/share/nginx/html;
index index.html index.htm;
#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 /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;
#
# 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;
#
#
server
listen 81;
server_name localhost;
location /
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#禁用缓存
proxy_buffering off;
#反向代理的地址
proxy_pass http://localhost:8080;
以上是关于nginx 初尝试的主要内容,如果未能解决你的问题,请参考以下文章