nginx架设更新服务器

2018-11-29  本文已影响0人  hexg1016

更新服务器采用nginx架设,需要http和https同时访问。

使用ssl模块配置同时支持http和https并存

一 nginx安装

Nginx要支持https,安装的时候需要ssl模块。下载nginx,解压,进入解压目录:

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

然后make &&make install

二 生成证书

# 1、首先,进入你想创建证书和私钥的目录,例如:

cd /etc/nginx/

# 2、创建服务器私钥,命令会让你输入一个口令:

openssl genrsa -des3 -out server.key 1024

# 3、创建签名请求的证书(CSR):

openssl req -new -key server.key -out server.csr

# 4、在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

cp server.key server.key.org

openssl rsa -in server.key.org -out server.key

# 5、最后标记证书使用上述私钥和CSR:

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

三 nginx文件路径配置方式

nginx指定文件路径有两种方式root和alias,root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。

最基本的区别

alias 指定的目录是准确的,给location指定一个目录。

root 指定目录的上级目录,并且该上级目录要含有locatoin指定名称的同名目录。

以root方式设置资源路径:

语法: root path;

配置块: http、server、location、if

以alias 方式设置资源路径

语法: alias path;

配置块: location

Example:

location /img/ {

alias /var/www/image/;

}

#若按照上述配置的话,则访问/img/目录里面的文件时,nginx会自动去/var/www/image/目录找文件

location /img/ {

root /var/www/image;

}

#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件

注意:

1.使用alias时,目录名后面一定要加”/“。

2.使用alias标签的目录块中不能使用rewrite的break。

3.alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。

4.alias只能位于location块中

四 修改配置文件

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

#gzip  on;

server {

listen      8089;

server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

#location / {

#    root  html;

#    index  index.html index.htm;

#}

location  /trunk/ {

#rewrite ^/eec/(.*)$ /$1 last;

alias /home/mobile_update/Chunks/;

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  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      8091 ssl;

server_name  localhost;

ssl                  on;

ssl_certificate    /usr/local/nginx/server.crt;

ssl_certificate_key  /usr/local/nginx/server.key;

ssl_session_cache    shared:SSL:1m;

ssl_session_timeout  5m;

ssl_ciphers  HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers  on;

location  /trunk/{

#rewrite ^/eec/(.*)$ /$1 last;

alias  /home/mobile_update/Chunks/;

#index  index.html index.htm;

}

}

}

上一篇 下一篇

猜你喜欢

热点阅读