nginx基础配置(rails)

蔡源茂 | 创建:2018-03-15 | 最后更新:2018-05-22 | 1102次阅读

安装nginx

$ sudo apt-get install nginx

启动nginx

$ sudo /etc/init.d/nginx start

查看nginx启动进程

$ ps  -ef|grep nginx
root     15779     1  0 17:28 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 15780 15779  0 17:28 ?        00:00:00 nginx: worker process
www-data 15781 15779  0 17:28 ?        00:00:00 nginx: worker process
caiyuan+ 16068 11770  0 17:44 pts/2    00:00:00 grep --color=auto nginx

配置最简单的反向代理

创建文件 /etc/nginx/conf.d/nginx.conf


server { # simple reverse-proxy
  listen       80;
  server_name  192.168.1.104;  #此处可以填写需要对外代理的服务器地址,填写域名或者ip

  # serve static files
  location ~ \.(js|css)  {  #填写静态文件匹配的正则表达式
    root    /home/xxxxxx/blog/public;   #静态文件在本机的位置
  }

  # pass requests for dynamic content to rails/turbogears/zope, et al
  location / {
    proxy_pass      http://192.168.1.104:3000;  #反向代理的服务器url
  }
}

增加了强制https的配置

  1. 查询nginx已经编译的模块信息$ nginx -V
  2. 增加ssl模块,请参考 http://blog.csdn.net/cin_ie/article/details/72843277
  3. 生成自己的ssl证书、申请免费的腾讯云ssl证书,请到文末查看参考链接

server {
    listen 80;
    server_name 192.168.1.104; #此处可以填写需要对外代理的服务器地址,填写域名或者ip
    rewrite ^(.*) https://$server_name$1 permanent;
}

server { # simple reverse-proxy
  listen 443;
  server_name  192.168.1.104;  #此处可以填写需要对外代理的服务器地址,填写域名或者ip

  ssl on;  
  ssl_certificate 1_nine-god.com_bundle.crt; #(证书公钥)  ,此文件需要放置在/etc/nginx目录下
  ssl_certificate_key 2_nine-god.com.key; #(证书私钥)   ,此文件需要放置在/etc/nginx目录下
  
  ssl_session_timeout 5m;  
  ssl_protocols  SSLv2 SSLv3 TLSv1;  
  ssl_ciphers  HIGH:!aNULL:!MD5;  
  ssl_prefer_server_ciphers on;  
 
  # serve static files
  location ~ \.(js|css|ico)  {  #填写静态文件匹配的正则表达式
    root    /home/xxxxxx/blog/public;   #静态文件在本机的位置
  }

  # pass requests for dynamic content to rails/turbogears/zope, et al
  location / {
    proxy_set_header Origin http://192.168.1.104:3000;  #设置报文header头部。devise 认证时会检验request.origin == request.base_url
    proxy_pass      http://192.168.1.104:3000;  #反向代理的服务器url
  }
}

检查配置文件,并重启nginx

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ sudo /etc/init.d/nginx stop
[ ok ] Stopping nginx (via systemctl): nginx.service.

$ sudo /etc/init.d/nginx start
[ ok ] Starting nginx (via systemctl): nginx.service.


参考链接:
(1)http://wiki.ubuntu.org.cn/Nginx
(2)https://www.nginx.com/resources/wiki/start/topics/examples/full/
(3)http://blog.csdn.net/w410589502/article/details/72833283 (ssl证书生成)
(4)https://cloud.tencent.com/document/product/400/8422 (腾讯云免费证书申请)


共收到 0 条回复


评论:

忘记密码?
其他账号登录: QQ登录

回到顶部
到达底部