nginxsip 401 unauthorizedd怎么解决

基于Nginx的Openresty基本api参考
ngx_lua模块(原理实现)&1、每个worker(工作进程)创建一个LuaVM,worker内所有协程共享VM;2、将NginxI/O原语封装后注入LuaVM,允许Lua代码直...
ngx_ 模块(原理实现)&1、每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM;2、将 I/O原语封装后注入 Lua VM,允许Lua代码直接访问;3、每个外部请求都由一个Lua协程处理,协程之间数据隔离;4、Lua代码调用I/O操作等异步接口时,会挂起当前协程(并保护上下文数据),而不阻塞worker;5、I/O等异步操作完成时还原相关协程上下文数据,并继续运行;&ngx_ 模块提供的指令和API等:指令名称说明_use_default_type是否使用default_type指令定义的Content-Type默认值_code_cache*_by__file文件是否cache_regex_cache_max_entries_regex_match_limit_package_path用Lua写的外部库路径(.文件)lua_package_cpath用C写的lua外部库路径(.so文件)init_by_luamaster进程启动时挂载的lua代码init_by_lua_fileinit_worker_by_luaworker进程启动时挂载的lua代码,常用来执行一些定时器任务init_worker_by_lua_fileset_by_lua设置变量set_by_lua_filecontent_by_luahandler模块content_by_lua_filerewrite_by_luarewrite_by_lua_fileaccess_by_luaaccess_by_lua_fileheader_filter_by_luaheader filter模块header_filter_by_lua_filebody_filter_by_luabody filter模块,ngx.arg[1]代表输入的chunk,ngx.arg[2]代表当前chunk是否为lastbody_filter_by_lua_filelog_by_lualog_by_lua_filelua_need_request_body是否读请求体,跟ngx.req.read_body()函数作用类似lua_shared_dict创建全局共享的table(多个worker进程共享)lua_socket_connect_timeoutTCP/unix 域socket对象connect方法的超时时间lua_socket_send_timeoutTCP/unix 域socket对象send方法的超时时间lua_socket_send_lowat设置cosocket send buffer的low water值lua_socket_read_timeoutTCP/unix 域socket对象receive方法的超时时间lua_socket_buffer_sizecosocket读buffer大小lua_socket_pool_sizecosocket连接池大小lua_socket_keepalive_timeoutcosocket长连接超时时间lua_socket_log_errors是否打开cosocket错误日志lua_ssl_cipherslua_ssl_crllua_ssl_protocolslua_ssl_trusted_certificatelua_ssl_verify_depthlua_http10_bufferingrewrite_by_lua_no_postponelua_transform_underscores_in_response_headerslua_check_client_abort是否监视client提前关闭请求的事件,如果打开监视,会调用ngx.on_abort()注册的回调lua_max_pending_timerslua_max_running_timers&&&table说明ngx.arg指令参数,如跟在content_by_lua_file后面的参数ngx.var变量,ngx.var.VARIABLE引用某个变量ngx.ctx请求的lua上下文ngx.header响应头,ngx.header.HEADER引用某个头ngx.status响应码API说明ngx.log输出到error.logprint等价于 ngx.log(ngx.NOTICE, ...)ngx.send_headers发送响应头ngx.headers_sent响应头是否已发送ngx.resp.get_headers获取响应头ngx.timer.at注册定时器事件ngx.is_subrequest当前请求是否是子请求ngx.location.capture发布一个子请求ngx.location.capture_multi发布多个子请求ngx.execngx.redirectngx.print输出响应ngx.say输出响应,自动添加'\n'ngx.flush刷新响应ngx.exit结束请求ngx.eofngx.sleep无阻塞的休眠(使用定时器实现)ngx.get_phasengx.on_abort注册client断开请求时的回调函数ndk.set_var.DIRECTIVEngx.req.start_time请求的开始时间ngx.req.http_version请求的HTTP版本号ngx.req.raw_header请求头(包括请求行)ngx.req.get_method请求方法ngx.req.set_method请求方法重载ngx.req.set_uri请求URL重写ngx.req.set_uri_argsngx.req.get_uri_args获取请求参数ngx.req.get_post_args获取请求表单ngx.req.get_headers获取请求头ngx.req.set_headerngx.req.clear_headerngx.req.read_body读取请求体ngx.req.discard_body扔掉请求体ngx.req.get_body_datangx.req.get_body_filengx.req.set_body_datangx.req.set_body_filengx.req.init_bodyngx.req.append_bodyngx.req.finish_bodyngx.req.socketngx.escape_uri字符串的url编码ngx.unescape_uri字符串url解码ngx.encode_args将table编码为一个参数字符串ngx.decode_args将参数字符串编码为一个tablengx.encode_base64字符串的base64编码ngx.decode_base64字符串的base64解码ngx.crc32_short字符串的crs32_short哈希ngx.crc32_long字符串的crs32_long哈希ngx.hmac_sha1字符串的hmac_sha1哈希ngx.md5返回16进制MD5ngx.md5_bin返回2进制MD5ngx.sha1_bin返回2进制sha1哈希值ngx.quote_sql_str语句转义ngx.today返回当前日期ngx.time返回UNIX时间戳ngx.now返回当前时间ngx.update_time刷新时间后再返回ngx.localtimengx.utctimengx.cookie_time返回的时间可用于cookie值ngx.http_time返回的时间可用于HTTP头ngx.parse_http_time解析HTTP头的时间ngx.re.matchngx.re.findngx.re.gmatchngx.re.subngx.re.gsubngx.shared.DICTngx.shared.DICT.getngx.shared.DICT.get_stalengx.shared.DICT.setngx.shared.DICT.safe_setngx.shared.DICT.addngx.shared.DICT.safe_addngx.shared.DICT.replacengx.shared.DICT.deletengx.shared.DICT.incrngx.shared.DICT.flush_allngx.shared.DICT.flush_expiredngx.shared.DICT.get_keysngx.socket.udpudpsock:setpeernameudpsock:sendudpsock:receiveudpsock:closeudpsock:settimeoutngx.socket.tcptcpsock:connecttcpsock:sslhandshaketcpsock:sendtcpsock:receivetcpsock:receiveuntiltcpsock:closetcpsock:settimeouttcpsock:setoptiontcpsock:setkeepalivetcpsock:getreusedtimesngx.socket.connectngx.thread.spawnngx.thread.waitngx.thread.killcoroutine.createcoroutine.resumecoroutine.yieldcoroutine.wrapcoroutine.runningcoroutine.statusngx.config.debug编译时是否有 --with-debug选项ngx.config.prefix编译时的 --prefix选项ngx.config._version返回版本号ngx.config._configure返回编译时 ./configure的命令行选项ngx.config.ngx_lua_version返回ngx_lua模块版本号ngx.worker.exiting当前worker进程是否正在关闭(如reload、shutdown期间)ngx.worker.pid返回当前worker进程的pid常量说明Core constantsngx.OK (0)ngx.ERROR (-1)ngx.AGAIN (-2)ngx.DONE (-4)ngx.DECLINED (-5)ngx.nilHTTP method constantsngx.HTTP_GETngx.HTTP_HEADngx.HTTP_PUTngx.HTTP_POSTngx.HTTP_DELETEngx.HTTP_OPTIONS&&ngx.HTTP_MKCOL&&&&ngx.HTTP_COPY&&&&&&ngx.HTTP_MOVE&&&&&ngx.HTTP_PROPFIND&ngx.HTTP_PROPPATCH&ngx.HTTP_LOCK&ngx.HTTP_UNLOCK&&&&ngx.HTTP_PATCH&&&ngx.HTTP_TRACE&&HTTP status constantsngx.HTTP_OK (200)ngx.HTTP_CREATED (201)ngx.HTTP_SPECIAL_RESPONSE (300)ngx.HTTP_MOVED_PERMANENTLY (301)ngx.HTTP_MOVED_TEMPORARILY (302)ngx.HTTP_SEE_OTHER (303)ngx.HTTP_NOT_MODIFIED (304)ngx.HTTP_BAD_REQUEST (400)ngx.HTTP_UNAUTHORIZED (401)ngx.HTTP_FORBIDDEN (403)ngx.HTTP_NOT_FOUND (404)ngx.HTTP_NOT_ALLOWED (405)ngx.HTTP_GONE (410)ngx.HTTP_INTERNAL_SERVER_ERROR (500)ngx.HTTP_METHOD_NOT_IMPLEMENTED (501)ngx.HTTP_SERVICE_UNAVAILABLE (503)ngx.HTTP_GATEWAY_TIMEOUT (504)& log level constantsngx.STDERRngx.EMERGngx.ALERTngx.CRITngx.ERRngx.WARNngx.NOTICEngx.DEBUGhttp status code 401 - NGINX | NetworkError: 401 Unauthorized ~ but it works? - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.0 million programmers, just like you, helping each other.
J it only takes a minute:
I've a really strange issue mostly with images and sometimes with URLs, too. (on my NGINX setup)
Sometimes I find following message in the console of Firebug the message:
"NetworkError: 401 Unauthorized - /images//sample1.png"
"NetworkError: 401 Unauthorized - /appliaction/item/18/#"
But the strange thing is, these things are loaded! There aren't anything missing. So I'm a little bit confused about this error message.
Configuration:
myproject-file in "sites-available" and "sites-enabled"
listen 80;
#listen 443
root /srv/www/
access_log /var/log/nginx/myproject-access.
error_log /var/log/nginx/myproject-error.
include global/dev.
## Disable Access
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.
## Open instead listing (start)
index index.php index.html index.
## Redirect Default Pages
# error_page 404 /404.
## favicon.ico should not be logged
location = /favicon.ico {
## Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
## Deny all attems to access possible configuration files
location ~ \.(tpl|yml|ini|log)$ {
## XML Sitemap support.
location = /sitemap.xml {
## robots.txt support.
location = /robots.txt {
location ~ \.php$ {
try_files $uri $uri/ =404;
## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_split_path_info ^(.+\.php)(/.+)$;
## required for upstream keepalive
# disabled due to failed connections
#fastcgi_keep_
include fastcgi_
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
client_max_body_size 24M;
client_body_buffer_size 128k;
## Timeout for Nginx to 5 min
fastcgi_read_timeout 300;
## upstream "php-fpm" must be configured in http context
fastcgi_pass php-
#URL Rewrite
location / {
try_files $uri $uri/ /index.php?$
Know someone who can answer? Share a link to this
via , , , or .
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Browse other questions tagged
rev .25642
Stack Overflow works best with JavaScript enabledcallback - 401 unauthorized error handling in AngularJS - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.0 million programmers, just like you, helping each other.
J it only takes a minute:
I'm very newbie in AngularJS, and now spending 3 days in finding a way to handle 401 status. I've tried interceptors, using $http, using $resource...but nothing is working. My app calls JSONP call on the same server. when error occurs it is caught in error callback function. but the status is always 0 and the response is undefined.
First, I tried this interceptor
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.responseInterceptors.push(['$q', function($q) {
return function(promise) {
return promise.then(function(response) {
console.log('success in interceptor');
}, function(response) {
console.log('error in interceptor');
console.log(response);
if (response.status === 401) {
response.data = {
status: false,
description: 'Authentication required!'
return $q.reject(response);
Second, also tried in controllers using $resource
$scope.fetchData = function(fromDate, toDate){
Cancel.get({from: fromDate, to: toDate, perPage: 99999},
function(data){
$scope.cancels
= $scope.filteredCancels = data.
$scope.search();
function(response) {
$scope.errorMessage = '&h4&Error : '+response.status+'&/h4&';
window.location = "/";
Third, tried using $http instead of $resource
$scope.fetchData = function(fromDate, toDate){
$http.jsonp('http://host:8900/api/cancellations?callback=JSON_CALLBACK')
.success(function(data, status, headers, config) {
console.log(status);
.error(function(data, status, headers, config) {
console.log(status);
Here is header information for the JSONP call
Request URL:http://host:8900/api/cancellations?callback=angular.callbacks._0
Request Method:GET
Status Code:401 Unauthorized
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Cookie:__utma=; __utmc=; __utmz=.2.utmcsr=cyphersmart.:8900|utmccn=(referral)|utmcmd=referral|utmcct=/; remember_username=elie.; PHPSESSID=gdoemlp5jltqq62etc5gfuh653; cookie= __utma=1.; __utmb=1.3.10.; __utmc=1; __utmz=1..1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Host:host:8900
Referer:http://host:8900/reports/cancels/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0. Chrome/25.0. Safari/537.22
Query String Parametersview sourceview URL encoded
callback:angular.callbacks._0
Response Headersview source
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Type:application/ charset=utf-8
Date:Fri, 06 Sep :13 GMT
Expires:Thu, 19 Nov :00 GMT
Keep-Alive:timeout=20
Pragma:no-cache
Server:nginx/0.7.65
Transfer-Encoding:chunked
I couldn't find a way to handle the unauthorized status 401, I tied all the things though.
It would be very appreciated if I can get a tip or kind advice.
6,0601360121
i needed to do very similar recently, here is my interceptor
app.factory("HttpErrorInterceptorModule", ["$q", "$rootScope", "$location",
function($q, $rootScope, $location) {
var success = function(response) {
// pass through
error = function(response) {
if(response.status === 401) {
// dostuff
return $q.reject(response);
return function(httpPromise) {
return httpPromise.then(success, error);
]).config(["$httpProvider",
function($httpProvider) {
$httpProvider.responseInterceptors.push("HttpErrorInterceptorModule");
modified slight for your use case
Follows a similar solution ...
angular.module('myApp', ['myApp.services', 'myApp.directives'], function ($routeProvider, $locationProvider, $httpProvider, $location) {
var httpInterceptor = ['$rootScope', '$q', function (scope, $q) {
function success(response) {
function error(response) {
var status = response.
if (status == 401) {
$location.url('/login');
return $q.reject(response);
return function (promise) {
return promise.then(success, error);
$httpProvider.responseInterceptors.push(httpInterceptor);
In case of any API call returns 401 we have to redirect user to login page. Angular’s HTTP interceptor is great for that job. As you can see from app.js above, it’s been pushed to pipe here:
httpProvider.responseInterceptors.push('httpInterceptor');
The interceptor implementation itself,
'use strict';
angular.module('dashboardApp').factory('httpInterceptor', function httpInterceptor ($q, $window, $location) {
return function (promise) {
var success = function (response) {
var error = function (response) {
if (response.status === 401) {
$location.url('/login');
return $q.reject(response);
return promise.then(success, error);
The accepted answer doesn't work for later versions of angular. Using 1.5.x (and maybe even earlier) you need to write the interceptor differently:
// http interceptor to handle redirection to login on 401 response from API
app.factory('httpResponseInterceptor', ['$q', '$rootScope', '$location', function($q, $rootScope, $location) {
responseError: function(rejection) {
if (rejection.status === 401) {
// Something like below:
$location.path('signin/invalidSession');
return $q.reject(rejection);
Apply with:
app.config(function($httpProvider) {
$httpProvider.interceptors.push('httpResponseInterceptor');
See here for further information $http#interceptors
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .25642
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 401 unauthorized 的文章

 

随机推荐