首页C# 正文

nginx+iis使用

时间: 2019年6月5日 浏览 27175

一、nginx的介绍

    nginx是由俄罗斯人开发的一款高性能的http和反向代理服务器,也可以用来作为邮件代理。相比较于其他的服务器,具有占用内存少,稳定性高等优势

   Nginx相关地址

   源码:https://trac.nginx.org/nginx/browser

  官网:http://www.nginx.org/

二、nginx的配置

nginx常用命令:

nginx -s stop 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。

nginx -s quit 平稳关闭Nginx,保存相关信息,有安排的结束web服务。

nginx -s reload 因改变了Nginx相关配置,需要重新加载配置而重载。

nginx -s reopen 重新打开日志文件。

nginx -c filename 为 Nginx 指定一个配置文件,来代替缺省的。

nginx -t 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。

nginx -v 显示 nginx 的版本。 nginx -V 显示 nginx 的版本,编译器版本和配置参数。

   启动服务:

下载运行nginx.exe,启动服务

 

如图所示出现nginx进程则表示启动成功

通过命令行的方式启动:start nginx.exe

停止服务:nginx -s stop

重新加载配置:nginx -s  reload

或者直接运行 nginx.bat 如图:

 

 如果未启动服务成功可以查看logs文件下error文件日志

 

常见错误信息

No mapping for the Unicode character exists in the target multi-byte code page:表示文件目录中含有中文字符,建议直接放在C盘根目录下

An attempt was made to access a socket in a way forbidden by its access permissions:表示80端口,可以去配置将80端口更改为别的端口或者去注册表里面禁用80端口,这里不建议去注册表禁用80端口,因为禁用后iis所有的站点将会无法启动

这里是在本机测试,先去iis新建两个站点,端口分别为8081和8082

 

 

 

 

三、nginx的配置介绍

nginx主要配置文件是在conf/nginx.conf里面

主要配置信息如下

#user nobody;
worker_processes 4; #nginx进程数,建议设置为等于CPU总核心数

#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;

 #服务器集群名称为Jq_one
 upstream ngintest.com{
 server 127.0.0.1:8081 weight=4; 
 server 127.0.0.1:8082 weight=4; 
 }

 server {
 listen 8088;
 server_name ngintest.com;

 #charset koi8-r;

 #access_log logs/host.access.log main;

 location / {
 root html;
 index index.aspx index.html index.htm;
 #指向集群名称为Jq_one
 proxy_pass http://ngintest.com; 
 #设置主机头和客户端真实地址,以便服务器获取客户端真实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;
 }

 #静态资源缓存设置
 location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$
 { 
 expires 30d;
 root /nginx-1.9.3/html;#root:
 break;
 }


 #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;
 }

 # 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;
 # }
 #}

}
 upstream ngintest.com{
 server 127.0.0.1:8081 weight=4; 
 server 127.0.0.1:8082 weight=4; 
 }
表示要负载均衡的站点,多个站点可在里面增减,
weight表示访问权重值,值越大表示访问到的几率越大
 listen 8088;
表示nginx要监听的端口,这里由于iis默认会占用80端口,所以这里要改成除80以外端口
server_name ngintest.com;
服务器名称,这里服务器名称要和upstream后面的名称一致
全部配置好后重启nginx服务

浏览器访问:127.0.0.1:8088,多次刷新会在两个站点来回切换